Thread: Clock Troubles

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    6

    Clock Troubles

    I appear to be having trouble with clock(). I need to get precise time measurements (milliseconds would be nice, but I understand that that is not universally possible) and I would like to remain operating system unspecific. I decided to use clock(), but it has not been working as advertised.

    The output of the following program is simply '0'. Yet when I increase the size of the for loop, the value becomes positive -- but is always an even number of seconds. Apparently, clock() is acting as if it has a resolution of seconds or worse. What do I do to make this work?

    Code:
      1 #include <stdio.h>
      2 #include <time.h>
      3 
      4 int main(int argc, char** argv)
      5 {
      6     clock_t a = clock();
      7     int i; for(i = 0; i < 10000; i++);
      8     clock_t b = clock();
      9     printf("%f\n",(double)(b-a));
     10     return 0;
     11 }

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by _Nate_ View Post
    I appear to be having trouble with clock(). I need to get precise time measurements (milliseconds would be nice, but I understand that that is not universally possible) and I would like to remain operating system unspecific. I decided to use clock(), but it has not been working as advertised.

    The output of the following program is simply '0'. Yet when I increase the size of the for loop, the value becomes positive -- but is always an even number of seconds. Apparently, clock() is acting as if it has a resolution of seconds or worse. What do I do to make this work?

    Code:
      1 #include <stdio.h>
      2 #include <time.h>
      3 
      4 int main(int argc, char** argv)
      5 {
      6     clock_t a = clock();
      7     int i; for(i = 0; i < 10000; i++);
      8     clock_t b = clock();
      9     printf("%f\n",(double)(b-a));
     10     return 0;
     11 }
    You need to use the macro CLOCKS_PER_SEC

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Apparently, clock() is acting as if it has a resolution of seconds or worse. What do I do to make this work?
    The following would print out seconds elapsed, so you could add this after the first printf():
    Code:
        printf("&#37;f\n",(double)(b-a)/CLOCKS_PER_SEC);
    The loop probably executes almost instantaneously. In fact the compiler may even optimise it out, since it's not doing any useful work. I think there's a POSIX function called ftime(), which has a milliseconds field.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    6
    Why does dividing by CLOCKS_PER_TICK help?

    Also, I too thought that the compiler might be optimizing the loop out -- but as I said, you jack the size of the loop high enough, and the program prints something like '3000' or '4000' -- but any less than that, and it's right back to '0' -- no intermediary values.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Its CLOCKS_PER_SEC, not CLOCKS_PER_TICK.

    http://www.acm.uiuc.edu/webmonkeys/b....15.html#clock

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    From the example I gave you, I think its more efficient to do this:

    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main(void)
    {
    	clock_t ticks1, ticks2;
    	
    	ticks1 = clock();
    	ticks2 = ticks1;
    	while( ticks2 - ticks1 < CLOCKS_PER_SEC)
    		ticks2=clock();
    
    	printf("Took &#37;ld ticks to wait one second. ticks2 = %ld, ticks1 = %ld\n",ticks2 - ticks1, ticks2, ticks1);
    	printf("This value should be the same as CLOCKS_PER_SEC which is %ld.\n",CLOCKS_PER_SEC);
    	return 0;
    }
    This way, you'll be sure that the while loop will take exactly 1 second if ticks1 is some other random, relatively big number like 264.
    Last edited by samus250; 06-17-2008 at 08:04 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is nothing wrong with the original code posted (it would show the number of clock-ticks, so dividing by CLOCKS_PER_SEC would give the time in seconds).

    The problem with the time being zero is either that the time of that loop is so short that it's not registering even one tick - make the loop longer. But it may well be that the compiler removes the empty loop as it's not actually doing anything.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Quote Originally Posted by _Nate_ View Post
    but is always an even number of seconds. Apparently, clock() is acting as if it has a resolution of seconds or worse.
    clock() measures the ticks that have passed since program start. The interval of the ticks however depends on the resolution of the operating system's timer.
    I don't know what the frequency is on Windows, but I guess it'll be between 250 and 500Hz.
    500Hz would mean that a tick occurs every 2ms, 250Hz means a tick every 4ms. So it'll probably be enough for measuring ms, but it will not be too acurately though

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    6
    So what would be the best way going about getting millisecond precision?

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by _Nate_ View Post
    So what would be the best way going about getting millisecond precision?
    Just do something like this:
    Code:
    	ticks = clock(); // gets the current ticks count
    	semiprimes = semiprimegen(parameters); // this function executes
    	ticks = clock() - ticks; // get the difference of ticks (results in the time the function ran)
    
    	// now print the time in seconds with two decimal points
    	printf("Took &#37;.2f seconds to generate semiprimes.\n", ticks / (double)CLOCKS_PER_SEC);
    Hope it helps.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Err, I don't think you can actually make your own function that sleeps. Not in simple way. You would need to tell the OS to suspend your program. So you would still need a function for that.

    Why make your own function for that? I can only think of two things:
    1) Don't what to sleep the program for X seconds, but inform when you want it to sleep and wake-up. In this case there should be other functions like sleep_wait_for_signal.

    2) You want it to work with less CPU-power. In this case you are looking for something else. There was a thread about that.

    Why do you want to make your own sleep() if I may ask?

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    nahh, I was just curious.

    Thanks!

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    The same principle is used for WaitForXXXXObject where there is a timeout value involved. So if you want to do a Sleep of your own (that doesn't use CPU time), then you could use WaitForSingleObject() with a timeout to match the Sleep time, and give a event to wait for that never gets signalled. But that's in no way better than the Sleep function itself - just different.
    In some way - it is better, WaitForSingleObject has precision of 1 ms while Sleep - about 10ms

    Instead of event - handle of the current process or thread could be used - they are not signaled till the process (thread) is finished - so no need to create event...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    In some way - it is better, WaitForSingleObject has precision of 1 ms while Sleep - about 10ms

    Instead of event - handle of the current process or thread could be used - they are not signaled till the process (thread) is finished - so no need to create event...
    Good points.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  4. world clock
    By nevermind in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 07:45 AM
  5. ANN: The Fourth Contest: Alarm Clock, sign up here
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 59
    Last Post: 08-10-2002, 12:24 AM