Sunday, January 09, 2011

Time measurement methods

I've just recently tried to decide what is the best way to measure times. The followings are a short summary I got after browsing a few sources in the Internet (See references).

1. gettimeofday
-. Resolution : 1 microsecond
-. Have a negative time issue due to clock reset or NTP

2. clock_gettime(CLOCK_MONOTONIC)
-. Resolution : 1 nanosecond
-. Use HPET, an external chipset on board.
-. An optimal choice, considering both reliability and portability

3. RDTSC
-. Resolution : 1 nanosecond
-. Use RDTSC, a cpu instruction
-. Very light-weight but lots of issues on reliability
-. For a short run and if it is clear on the reliability issues (see Ref 2.), it's good to use.

References:
1. http://aufather.wordpress.com/2010/09/08/high-performance-time-measuremen-in-linux/
2. http://juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html
3. http://www.kerrywong.com/2009/05/28/timing-methods-in-c-under-linux/

No comments: