473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Precision of clock()

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

Aug 30 '05 #1
1 4540
Peger, Daniel H. wrote:
<snip>
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
Aug 30 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Gino Elloso - Philippines | last post by:
Hello, I made a webpage ( @ Geocities Free Webpages ) that contains a mousetrail clock script. I simply copied the script ( including all html tags ) exactly as it was from a source webpage (...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
0
by: Peger, Daniel H. | last post by:
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 meassurable time period is 0.01 seconds, but for my...
5
by: Robert | last post by:
Hello all, I use clock() to measure the time cost in my program. But the t_clock always be the multiply of 10, for example 10, 20, 30, 40,... And I want to get the precision of 1, such as 11,...
5
by: Pushkar Pradhan | last post by:
I've decided to use clock() to time my routines, my code is like this: clk1 = clock(); while (n != 1000000) { mm_2r2c_2r2c_bc(&a, &b, &c); n++; } clk2 = clock(); t2 = time(NULL);...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
54
by: CoreyWhite | last post by:
The following experiment is a demonstration of TIME TRAVEL. When writing this program, and testing it out I found that sometimes the program would engage itself in time travel but other times it...
12
by: cenktarhancenk | last post by:
is there a way to display a ticking clock in a web page using javascript? but not in a textbox, rather as text that i can change the style, font etc. cenk tarhan
5
by: none | last post by:
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"
2
by: =?GB2312?B?zPC5zw==?= | last post by:
2008/10/29 James Mills <prologic@shortcircuit.net.au>: Well, whatelse can I use? ShenLei
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.