Reputation: 5642
I am interested in accurately timing a c++ application. There seems to be multiple definitions for "time", but for the sake of this question... I am interested in the time that I am counting on my watch in the real world... if that makes any sense! Anyway, in my application, my start time is done like this:
clock_t start = clock();
.
.
. // some statements
.
.
clock_t end = clock();
.
.
double duration = end - start;
.
.
cout << CLOCKS_PER_SEC << endl;
start is equal to 184000 end is equal to 188000 CLOCKS_PER_SEC is equal to 1000000
Does this mean the duration (in seconds) is equal to 4000/1000000 ? If so, this would mean the duration is .004 seconds? Is there a more accurate way of measuring this?
Thank you
Upvotes: 3
Views: 3921
Reputation: 141
Try this to find the time in nanoseconds precision
struct timespec start, end;
clock_gettime(CLOCK_REALTIME,&start);
/* Do something */
clock_gettime(CLOCK_REALTIME,&end);
It returns a value as ((((unsigned64)start.tv_sec) * ((unsigned64)(1000000000L))) + ((unsigned64)(start.tv_nsec))))
If you find this helpful kindly refer this link too..
Upvotes: 1