time.h function clock() only returning 0
Brian Bober
netdemonz at yahoo.com
Thu Apr 8 07:44:00 UTC 2004
Ulrich Drepper wrote:
>
> clock() does not measure wall time, it measures CPU time of the process.
> You should be thankful that the value is always zero since this means
> sleep() is not implemented as a busy loop. Write some real code which
> doesn't spend all the time in the kernel and you'll see nonzero values
> returned.
Ok, done... But I have another question... Why does it always return even
numbers? Is it because of system timer resolution? I doubt it, because this
call seems to only have a resolution of 10 milliseconds as tested (notice the
changing number). Is it because of alloted timeslice (round-robin process
scheduling)? Can't be, because regardless of the timeslice, this is running
time as you said, and chances are small that the code would always run aligned
to a nice even value of processor time like that.
[boberb at netdragon1 gettimeofday]$ ./clock
Clock=0
Clock=360000
[boberb at netdragon1 gettimeofday]$ ./clock
Clock=0
Clock=370000
[boberb at netdragon1 gettimeofday]$ ./clock
Clock=0
Clock=360000
Code:
#include <time.h>
#include <iostream>
int main(void)
{
std::cout<<"Clock="<<clock()<<std::endl;
for(int i=0; i<100;i++)
{
for (int j=0; j<1000; j++)
{
for (int k=0; k<1000; k++)
{
int l=1+i+j+k;
}
}
}
std::cout<<"Clock="<<clock()<<std::endl;
}
More information about the fedora-test-list
mailing list