?
Solved

Inconsistent timing using clock_t

Posted on 2003-02-19
7
Medium Priority
?
328 Views
Last Modified: 2012-06-21
Part of my program is something like this:

{
....
//encoding timer starts
enc_start = clock();

Flush_Buffer(tab->len);

//encoding timer stops
enc_finish = clock();
duration[2] += (double)(enc_finish - enc_start);

//At the end of the program, i print out the total timing
duration[2] /= CLOCKS_PER_SEC;
printf("\nEncoding time = %2.3f secs", duration[2]);
....
}

The problem is that I'm getting different timing everytime I run the program. And the difference can be relatively big.
Is there any way to get consistent timing? or other method other than averaging the timing?

Thank you
0
Comment
Question by:m1ck3y
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 7985053
Can you provide an example of durations ?
Your method is pretty good. clock() has resolution in DOS ~ 55 ms and in Windows ~ 1 ms.
I suggest you to calculate your durations in clocks and not in seconds - you will not need to divide and to loose precision.
0
 
LVL 6

Expert Comment

by:gj62
ID: 7985184
When you say the differences are relatively large, how large?  How long does the process last (less than a second, several seconds, etc).

Timing is affected by many things, especially on Windows - system processes, other processes, etc.  The other thing that can throw off timing is if you run in debug mode.  Compile without debug, close all non-needed processes, and see if that helps.

0
 
LVL 5

Expert Comment

by:Kocil
ID: 7985415
Does the Flush_Buffer() do I/O to the disk ?
That may give you different timing because
seeking, chacing, fragmentation, etc.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
Kryp earned 280 total points
ID: 7986989
> The problem is that I'm getting different timing everytime I run the program.
Your program is not the only thing happening on your machine - unless its DOS
Any kind of windows / unix operating system has a number of background tasks which all take time.

What does Flush_Buffer() do?
If it accesses large blocks of memory, then at best it will have to wait for the processor to refill the data cache.
At worst it will have to wait for the OS to reload the correct page from the swap file.

If it's performing any kind of I/O, then it will definitely have to wait for the hard disk.  Somewhere along the line, you have to wait for the disk head to move to the right track, then wait for the right sector to rotate round until its underneath the head before data can be read/written.  The average seek time (move the head + wait for the rotation) of a disk is around 10ms - thats 10,000,000 clock ticks of your 1Ghz machine (an awful lot of instructions).

All of these add a random delay to the execution of your code, and if you add enough small (and random) delays to the execution of your code, this can add up to what appears to be a large overall difference.


0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 7987323

Your platform could be an issue, too.

I seem to recall that the DOS clock on the older PCs had a granularity or 7/100ths or 14/100ths of a second.



Kdo
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10006299
Nothing has happened on this question in over 7 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Kryp.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question