Any idea why clock() returns 0 or 10000

N

none

Hello,
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?
thank you.

I am using:
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

Thanks you in advance.

John
 
J

Jim Langston

none said:
Hello,
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?
thank you.

I am using:
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).
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.

Try something like:

for ( int i = 0; i < 100; ++i )
cout << "clock is " << clock() << "\n";

and you should see it change
 
N

none

Jim said:
Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).
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.

Try something like:

for ( int i = 0; i < 100; ++i )
cout << "clock is " << clock() << "\n";

and you should see it change
Thanks for the reply Jim,
Unfortunately it results in 100 x "clock is 0", or 100 x "clock is 10000"

Interesting.
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top