473,407 Members | 2,546 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,407 software developers and data experts.

clock() for timing

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);
printf("%g\n", clk2/CLOCKS_PER_SEC - clk1/CLOCKS_PER_SEC);

Result:
3.38969e-319

When I printed out CLOCKS_PER_SEC it is 999424.

What is the correct way to use clock() to time a routine?
Nov 13 '05 #1
5 5920
Pushkar Pradhan wrote:

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);
printf("%g\n", clk2/CLOCKS_PER_SEC - clk1/CLOCKS_PER_SEC);

Result:
3.38969e-319

When I printed out CLOCKS_PER_SEC it is 999424.

What is the correct way to use clock() to time a routine?


long unsigned loop;
clock_t start, stop;

start = clock();
while (start == clock()) {
;
}
start = clock();
for (loop = 0; loop != LOOPS; ++loop) {
mm_2r2c_2r2c_bc(&a, &b, &c);
}
stop = clock();
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
start = clock();
while (start == clock()) {
;
}
start = clock();
for (loop = 0; loop != LOOPS; ++loop) {
dummy_function(&a, &b, &c);
}
stop = clock();
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);

--
pete
Nov 13 '05 #2
pu*******@hotmail.com (Pushkar Pradhan) wrote:
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);
printf("%g\n", clk2/CLOCKS_PER_SEC - clk1/CLOCKS_PER_SEC);
For starters, are you quite sure that clock_t is a double on your
system?
Result:
3.38969e-319

When I printed out CLOCKS_PER_SEC it is 999424.

What is the correct way to use clock() to time a routine?


The above (although I'd have used a for loop, or at least while (n++ !=
1000000), but that's a matter of taste).
However, the way to print out the result is more like this:

printf("Seconds taken: %g\n", ((double)clk2 - clk1)/CLOCKS_PER_SEC));

I'd do the subtraction first, and the division after, because that may
be less likely to lead to rounding errors; and the cast to double is
necessary both to ensure that you don't do the calculation in an integer
type, which could lose precision, and to make sure that you pass the
right type to printf().

Richard
Nov 13 '05 #3
pete wrote:
Pushkar Pradhan wrote:
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);
printf("%g\n", clk2/CLOCKS_PER_SEC - clk1/CLOCKS_PER_SEC);

Result:
3.38969e-319

When I printed out CLOCKS_PER_SEC it is 999424.

What is the correct way to use clock() to time a routine?


long unsigned loop;
clock_t start, stop;

start = clock();
while (start == clock()) {
;
}
start = clock();


This looks silly. You want to find the begin of a clock interval, I suppose.
What do you want to do at the end of the loop? How will you figure out where
you are between two clock() values?
If you can't, the above code *is* silly.

Jirka

Nov 13 '05 #4
Jirka Klaue wrote:

pete wrote:
Pushkar Pradhan wrote:
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);
printf("%g\n", clk2/CLOCKS_PER_SEC - clk1/CLOCKS_PER_SEC);

Result:
3.38969e-319

When I printed out CLOCKS_PER_SEC it is 999424.

What is the correct way to use clock() to time a routine?


long unsigned loop;
clock_t start, stop;

start = clock();
while (start == clock()) {
;
}
start = clock();


This looks silly. You want to find the begin of a clock interval,
I suppose.
What do you want to do at the end of the loop?
How will you figure out where
you are between two clock() values?
If you can't, the above code *is* silly.


It prevents the faster of two events from having a slower time.
I'm more interested in knowing relative speeds of various methods,
rather than the exact time.
The advantage of not eliminating timing error where I can,
is just exactly what now ?

--
pete
Nov 13 '05 #5
In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
The advantage of not eliminating timing error where I can,
is just exactly what now ?


The right thing is to time intervals large enough for such kind of errors
to be irrelevant. Otherwise, other errors, which cannot be eliminated
(most implementations of clock() will also catch system activity not
directly related to your program) will reduce the accuracy of your
timing.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

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

Similar topics

3
by: James Harriman | last post by:
Hi, I need to be able to measure a time interval in milliseconds on a windows machine. I have tried using time.clock() but it appears to measure time in seconds...Is there a way to measure time...
13
by: Peter Hansen | last post by:
I would like to determine the "actual" elapsed time of an operation which could take place during a time change, in a platform-independent manner (at least across Linux/Windows machines). Using...
2
by: Oplec | last post by:
Hello, I am learning C++ using The C++ Programming : Special Edition by Bjarne Stroustrup. After reading some of his papers on his site, I came across a source code file that is used to compare...
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...
2
by: Randy Shore | last post by:
I have developed an Access application that replaces the paper scoresheet scoring for QuizBowl competitions with live, lap-top based scoring. Among other tasks the application displays a score...
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...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.