Precision of clock()

P

Peger, Daniel H.

Hi,
I'm having a slight problem with the precision of the standard c++ clock
function as it is defined in time.h .

On my system the shortest measurable time period is 0.01 seconds, but for
my programm I need to measure the processing time in ms.

Is there perhaps something like an environment variable controlling the
precision of clock(), or is there any other way of measuring time in ANSI
C in the ms domain??

Could running the code on a notebook with speedstep cause problems?? Idle
CPU-Freq is 600MHz and 100% is 1500MHz?

Regards, Daniel.

My code looks like this:

void test() {

for ( int i = 0; i<10000; i++) {

cout << "clock() = " << clock() << "\n";
}
}

and the output:

....
clock() = 190000
clock() = 190000
clock() = 200000
clock() = 200000
clock() = 200000
clock() = 200000
clock() = 200000
clock() = 200000
....
clock() = 210000
 
M

Marc Mutz

Peger, Daniel H. wrote:
On my system the shortest measurable time period is 0.01
seconds, but for my programm I need to measure the
processing time in ms.

Well, if your OS doesn't give you finer granularity, then
you have to use non-portable mechanisms, like performance
counter registers. Note that POSIX requires
CLOCKS_PER_SEC to be 1'000'000, independent of the actual
resolution. ANSI-C doesn't.
Is there perhaps something like an environment variable
controlling the precision of clock(), or is there any
other way of measuring time in ANSI C in the ms domain??

Note that clock() returns the CPU time, not the wall clock
time. Posix has gettimeofday() for wall clock time, which
also has 1ms granularity, but it's not guaranteed that
the OS supports this granularity.
Could running the code on a notebook with speedstep
cause problems?? Idle CPU-Freq is 600MHz and 100% is
1500MHz?
<snip>

Depends on the OS. Older Linux versions had a problem with
this, yes.

Marc
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top