View Full Version : How to write stopwatch function for benchmarking

6th August 2006, 03:59 PM
What functions do I use to write a stopwatch functions for benchmarking?

I want to retrieve the highest (most precise) time representation my machine is capable of (twice) and compute the difference and then display that difference in days, hours, minutes, seconds and fractions of a second. I want to display the start and stop times in year, month, day of month, hour, minute, seconds and fractions of seconds too.

I want to do this on both 64 bit linux and 32 bit linux with g++.


6th August 2006, 04:40 PM
The standard C library function from time.h is gettime.
It has nanosecond resolution. Use 'man gettime' and 'man time.h' for more info.

There is also a sys/time.h which defines an alternate subset of time-related functions such as gettimeofday. These use microsecond resolution.

Note, system's clock is probably not be precise at nanosecond resolution.