A platform-independent timer with microsecond accuracy:
start
timer::tic(); // run your code printf("elapsed seconds: %g\n", timer::toc());
Accurate and reliable measurement of performance involves several factors:
To take care of much of this boilerplate, timeit provides accurate and reliable estimates of both CPU or GPU code.
Here's a stripped down example of Monte-Carlo estimation of PI making use of timeit. Notice how it expects a void
function pointer.
#include <stdio.h> #include <arrayfire.h> using namespace af; void pi_function() { int n = 20e6; // 20 million random samples array x = randu(n,f32), y = randu(n,f32); // how many fell inside unit circle? float pi = 4.0 * sum<float>(sqrt(mul(x,x) + mul(y,y)) < 1) / n; } int main() { printf("pi_function took %g seconds\n", timeit(pi_function)); return 0; }
This produces:
pi_function took 0.007252 seconds
(Core i7 920 @ 2.67GHz with a Tesla C2070)
More examples can be found on our blog.