Re: Any idea why clock() returns 0 or 10000
On Jul 10, 1:27 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"none" <""john\"@(none)"> wrote in message
news:4693690c$0$15858$fa0fcedb@news.zen.co.uk...
can you help?
if I include:
#include <time.h>
and run the code:
clock_t t = clock();
cout << "t is " << t << endl;
I get (seemingly randomly) "t is 0" or "t is 10000"
Any ideas?
I am using:
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.
Knowing the OS would help as well. clock() usually resolves to
a system function.
Apparently you clock has a detail of 10,000ns (I think it's nano seconds?=
).
It depends on the system. And there are two separate issues:
units (i.e. 1/CLOCKS_PER_SEC) and resolution. The Posix
standard requires that CLOCKS_PER_SEC be 1000000, so the unit
will always be a microsecond. The resolution varies, but I
suspect that it's rarely this much.
The value you get on the first call to clock isn't specified,
either (although it shouldn't represent much more than the time
the program has already run), as does what clock() actually
measures.
clock() says how many clock ticks have happended since your
program started. since it's a short program, it usually
hasn't hit 10000 by the time clock_tt = clock(); is reached,
hence you get 0. Every now and then, maybe cause of other
background processes, it takes a little longer so you see the
tick over.
The actual clock ticks come at specific intervals, independantly
of when the program actually started. The actual clock ticks
are probably of much greater granularity than 1 microsecond, or
whatever. If the first clock tick happens to fall between the
start of his program and his call to clock(), he could get the
granularity. (Under Solaris, the first call to clock() always
returns 0, but this isn't true for other systems.)
Note too that clock() returns "the implementation\u2019s best
approximation to the processor time used by the program". A
system in which clock() increments due to activity by other
programs is broken.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34