By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

clock() for timing

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.