High-resolution timer for Windows, OSX, and Linux
A high-resolution timer for Windows, OS X, and Linux.
Performance timers are useful for profiling and optimizing code, but using one typically involves system specific calls. So, I wrapped these up and put them behind a simple API.
Simple example:
#include "tictoc.h"
int main(int argc, char* argv[])
{
TicTocTimer clock = tic();
time_consuming_operation();
printf("Elapsed time %f seconds.\n",toc(&clock)); // prints time since last tic()
another_time_consuming_operation();
printf("Elapsed time %f seconds.\n",toc(&clock)); // prints time since last toc()
}
An even simpler example:
#include "tictoc.h"
int main(int argc, char* argv[])
{
tic();
printf("%f\n",toc(0));
}
The last example uses a global timer. Using the global timer from different threads may produce strange results.
I wrote a little test suite to test the precision, but the tests are badly designed; they probably don't predict performance. I left them in the hope that one day they will be fixed!
The test suite runs tests for different resolutions that rely on some
implementation of usleep()
to pause for semi-controlled lengths of
time. However, this isn't a reliable way to measure the actual
precision of your system's timer.
You'll have to measure that for yourself according to what is appropriate for your application.