clock() for timing

  • Thread starter Pushkar Pradhan
  • Start date
P

Pushkar Pradhan

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?
 
P

pete

Pushkar said:
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);
 
R

Richard Bos

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
 
J

Jirka Klaue

pete said:
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
 
P

pete

Jirka said:
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 ?
 
D

Dan Pop

In said:
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
 

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
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top