Iron
Iron

Reputation: 1

IBM AIX std::clock()?

I execute in IBM AIX the following code.

int main(void)
{
    printf( "start\n");
    double time1 = (double)clock();      /* get initial time */
    time1 = time1 / CLOCKS_PER_SEC;      /*    in seconds    */

    boost::this_thread::sleep_for(boost::chrono::seconds(5));

    /* call clock a second time */
    double time2 = (((double)clock()) / CLOCKS_PER_SEC);
    double timedif = time2 - time1;
    printf( "The elapsed time is %lf seconds, time1:%lf time2:%lf CLOCKS_PER_SEC:%ld\n", 
            timedif));
}

The result is:

2018-04-07 09:58:37 start
2018-04-07 09:58:42 The elapsed time is 0.000180 seconds, time1:0.000000 time2:0.000181 CLOCKS_PER_SEC:1000000

I don't know why elapsed time is 0.000180 (why not 5)?

Upvotes: 0

Views: 180

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38784

According to the manual

Returns the processor time consumed by the program.

It is CPU time consumed by a program, it is not a physical time. A sleeping program does not consume CPU time. Thus in raw words, it is time interval from main till sleep plus time interval after sleep till return.

If you want to get system/real time, look at the std::chrono::system_clock class.

#include <chrono>
using std::chrono::system_clock;
system_clock::time_point time_now = system_clock::now();

Upvotes: 1

Related Questions