Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Timer

Status
Not open for further replies.

ernesth

Technical User
Jan 17, 2002
15
0
0
US
hello,
I am attempting to time some algorithms that I am testing, but, it seems as though I can't figure out how to time with enough precision (as you can probably guess, I keep getting run times of 0).
The method that I am using is as follows:
#include <time.h>

clock_t start, finish;
float runningTime;

start = clock();

//call function to be timed

finish = clock();

runningTime = (float) (finish-start)/CLOCKS_PER_SEC;

How do I get the time to be more precise? I have tried other forms of the method shown above, but have not had any real success. Most of the timing methods that I have looked up only deal with obtaining various forms of the date, or the time of day, not with being able to time small algorithms. Is there a method where I can call clock(), or some form of clock(), and receive the time in microseconds or nanoseconds?
Any help will be appreciated.

Thanks,
ErnestH
 
I did this once, but I don't remember if this is what I came up with. But, it's something to try in any case.

AFAIK,

runningTime = (float) (finish-start)/CLOCKS_PER_SEC;

calculates the time in seconds. To get a more precise timing, divide by a different number. For example, to get hundredths, try

runningTime = (float) (finish-start)/(CLOCKS_PER_SEC/100);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top