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  
 
 [ 4 post ] 

 Relevant Pages 

1. Tool for measuring speed of C code wanted

2. Measuring System Speed

3. Measure my hard disk speed.

4. 3D algorithm - help me speed it up!!!

5. Visual C++ 7 speed vs Visual C# speed

6. .NET speed vs COM speed

7. CPU Speed / CDROM speed

8. Pixels, Points and measuring strings

9. time measure of procedures

10. measuring (run) time

11. How to measure code size?

12. Measure time of operation

 

 
Powered by phpBB® Forum Software