Reputation: 325
I was trying to estimate the duration of a code block (particularly to get the time step of a physics-based animation for frame rate indepedency, so resolution should be in milliseconds grade). Questions here at stack overflow mostly recommend clock() method, I have tried it but it does not seem to work properly. I have solved the problem using gettimeoftheday() method, but still I'm curious as to why clock() fails. Here's my code block:
for (int i = 0; i < 10000; i++)
{
std::cout << "time: " << clock() << std::endl;
usleep(1000);
}
This prints time at a rate. When I increase the usleep value to, say 1000000, the increase rate of printed clock values drastically drop. I thought clock() should give a value related to an absolute time value, independent of my code.
Thanks in advance.
PS: To get a possible misunderstanding out of the way in advance, I'm not complaining the printing ratio has dropped. The VALUE printed at a certain delta time is not the same between usleep(1000) and usleep(1000000).
Upvotes: 0
Views: 1422
Reputation: 8020
The question assumptions are wrong but I had to upvote the question because time measurement and time APIs are confusing, so the user is right to ask such question.
Given the problem description, I would use the last ones for the calculation: set a 1 second interval timer that triggers a signal when done. Then print the number of frames you were able to draw. You can adjust frame rendering complexity with this number later.
Upvotes: 0
Reputation: 254461
I thought clock() should give a value related to an absolute time value
You thought wrong. From its specification (C99 7.32.2.1)
The
clock
function determines the processor time used.
Use time
, or a suitable clock from the std::chrono
library, or something less portable like clock_gettime
or gettimeofday
, if you want absolute time.
Upvotes: 1