In one of my application it gets timer a lot of times, so I must make sure which timer is faster.
I tested three common timer functions:
gettimeofday() (in windows we use GetLocalTime())
clock() (in windows we use GetTickCount())
time()
I tested them under unix/linux, each function called 1e6(1M or 1000k) times.
Result on my unix computer:
test_gettimeofday() 42.40ms test_clock() 344.27ms test_time() 183.65ms
Result on my Linux VPS:
test_gettimeofday() 190.00ms test_clock() 320.00ms test_time() 160.00ms
Result on my VoCore v0.3 (RT5350F MIPS 360MHz) board: ( it is slow 😀 )
test_gettimeofday() 1370.00ms test_clock() 1800.00ms test_time() 520.00ms
On my board, time() win, that is out of my expectation. OpenWrt are using uLibC but not glibC whose time() is calling gettimeofday(). uLibC must be using some other way.
But for average result, looks like gettimeofday() is the winner 🙂
source code download link: http://vonger.cn/upload/clock.c