Measuring algorithm speed
Author Message
Measuring algorithm speed

I want to measure the time taken to perform an algorithm.

time_t now, after, time_taken;

now = time(NULL);
/* do the algorithm here */
after = time(NULL);
time_taken = after - now;

Unfortunately the granularity of time_t is in seconds.
But the algorithm is performed in milliseconds.

Is there another way?

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.

Sun, 10 Feb 2002 03:00:00 GMT
Measuring algorithm speed

Quote:

> I want to measure the time taken to perform an algorithm.

> time_t now, after, time_taken;

> now = time(NULL);
> /* do the algorithm here */
> after = time(NULL);
> time_taken = after - now;

> Unfortunately the granularity of time_t is in seconds.
> But the algorithm is performed in milliseconds.

> Is there another way?

You could run through thousands of iterations, and then take the
time difference. There is no sub-second resolution time function
in ANSI C. You could've read this in the c.l.c FAQ:
<http://www.eskimo.com/~scs/C-faq/q19.37.html>

BTW, time_t is _not_ seconds, it's whatever is convenient for the
compiler, which might or might not be seconds; therefore, you can't
just subtract one from the other and expect a reasonable answer.
Use difftime().

Richard

Sun, 10 Feb 2002 03:00:00 GMT
Measuring algorithm speed

Quote:

>> I want to measure the time taken to perform an algorithm.

>> time_t now, after, time_taken;

>> now = time(NULL);
>> /* do the algorithm here */
>> after = time(NULL);
>> time_taken = after - now;

>> Unfortunately the granularity of time_t is in seconds.
>> But the algorithm is performed in milliseconds.

>> Is there another way?

----------------
You can improve the situation by using clock() in <time.h>.  But you will still
have to do many iterations to avoid error.  The time interval between 'ticks'
is a function of the OS. On DOS/Windows it is 18.2 ticks per second or about 55
msec. per tick.

Sun, 10 Feb 2002 03:00:00 GMT
Measuring algorithm speed

Quote:

>I want to measure the time taken to perform an algorithm.

>time_t now, after, time_taken;

>now = time(NULL);
>/* do the algorithm here */
>after = time(NULL);
>time_taken = after - now;

Usually it is better to use clock() rather than time() since this measures
CPU time rather than wall-clock time.

Quote:
>Unfortunately the granularity of time_t is in seconds.
>But the algorithm is performed in milliseconds.

>Is there another way?

The granularity of clock() is often subsecond but C makes no guarantees.
To time something that takes a short period you either find some special
feature of your particular system that allwos this or (more typically)
you repeat the operation a large number of times and measure the
overall time.

--
-----------------------------------------

-----------------------------------------

Sun, 10 Feb 2002 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages