2

I want to calculate time intervals (in 1/10th of 1 second) between some events happening in my program. Thus I use clock function for these needs like follows:

    clock_t begin;
    clock_t now;
    clock_t diff;

    begin = clock();

    while ( 1 )
    {
        now = clock();
        diff = now - begin;
        cout << diff / CLOCKS_PER_SEC << "\n";
        //usleep ( 1000000 );
    };

I expect the program to print 0 for 1 second, then 1 for 1 sec., then 2 for 1 sec. and so on... In fact it prints 0 for about 8 seconds, then 1 for about 8 seconds and so on...

By the way, if I add usleep in order program prints only 1 time per second, it prints only 0 all way long...

Great thanks for help!

Kolyunya
  • 5,973
  • 7
  • 46
  • 81

5 Answers5

2

The clock() function returns the amount of CPU time charged to your program. When you are blocked inside a usleep() call, no time is being charged to you, making it very clear why your time never seems to increase. As to why you seem to be taking 8 seconds to be charged one second -- there are other things going on within your system, consuming CPU time that you would like to be consuming but you must share the processor. clock() cannot be used to measure the passage of real time.

mah
  • 39,056
  • 9
  • 76
  • 93
  • thanks! Maybe you can advise the easiest way to measure real time in my C++ program? – Kolyunya Aug 18 '12 at 18:11
  • The `time()` function returns the current time, expressed as the number of seconds since January 1, 1970 UTC. `gettimeofday()` lets you get time in usecond resolution; see http://linux.die.net/man/2/gettimeofday. http://stackoverflow.com/questions/1468596/c-programming-calculate-elapsed-time-in-milliseconds-unix has an example using it. – mah Aug 18 '12 at 18:16
  • my great thanks for a reply! I know about `time`, but I need to measure `mseconds` or `useconds`, so I'm gonna read about `gettimeofday`, thanks for your advise! – Kolyunya Aug 18 '12 at 18:18
  • @Kolyunya C++11 has generalized clocks, including one that is supposed to have high resolution (`std::chrono::high_resolution_clock`). That might be a standard-compliant alternative to `gettimeofday`. – Philipp Aug 18 '12 at 18:32
1

I bet your printing so much to stdout that old prints are getting buffered. The buffer is growing and the output to the console can't keep up with your tight loop. By adding the sleep you're allowing the buffer some time to flush and catch up. So even though its 8 seconds into your program, your printing stuff from 8 seconds ago.

I'd suggest putting the actual timestamp into the print statement. See if the timestamp is lagging significantly from the actual time.

Doug T.
  • 64,223
  • 27
  • 138
  • 202
1

If you're able to use boost, checkout the Boost Timers library.

oz10
  • 153,307
  • 27
  • 93
  • 128
0

Maybe you have to typecast it to double.

cout << (double)(diff / CLOCKS_PER_SEC) << "\n";

Integers get rounded, probably to 0 in your case.

TheDudeAbides
  • 420
  • 1
  • 7
  • 21
  • -1 This really shouldn't matter. In one second, diff should be > CLOCKS_PER_SEC and integer division will result in 1. Thats what the OP is expecting. – Doug T. Aug 18 '12 at 18:09
0

Read about the time() function.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • It's best to read the other responses and then the comments about them before responding late to a post. – mah Aug 18 '12 at 19:18