1 Timing Your Code {#timing}
4 timer() : A platform-independent timer with microsecond accuracy:
5 * [timer::start()](\ref af::timer::start) starts a timer
7 * [timer::start()](\ref af::timer::stop) seconds since last \ref af::timer::start "start"
9 * \ref af::timer::stop(af::timer start) "timer::start(timer start)" seconds since 'start'
13 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
17 printf("elapsed seconds: %g\n", timer::stop());
18 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 Example: multiple timers
22 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
24 timer start1 = timer::start();
25 timer start2 = timer::start();
27 printf("elapsed seconds: %g\n", timer::stop(start1));
29 printf("elapsed seconds: %g\n", timer::stop(start2));
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 Accurate and reliable measurement of performance involves several factors:
33 * Executing enough iterations to achieve peak performance.
34 * Executing enough repetitions to amortize any overhead from system timers.
36 To take care of much of this boilerplate, [timeit](\ref af::timeit) provides
37 accurate and reliable estimates of both CPU or GPU code.
39 Here`s a stripped down example of
40 [Monte-Carlo estimation of PI](\ref benchmarks/pi.cpp) making use
41 of [timeit](\ref af::timeit). Notice how it expects a `void` function pointer.
43 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
45 #include <arrayfire.h>
49 int n = 20e6; // 20 million random samples
50 array x = randu(n,f32), y = randu(n,f32);
51 // how many fell inside unit circle?
52 float pi = 4.0 * sum<float>(sqrt(x*x + y*y)) < 1) / n;
56 printf("pi_function took %g seconds\n", timeit(pi_function));
59 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 pi_function took 0.007252 seconds
64 (test machine: Core i7 920 @ 2.67GHz with a Tesla C2070)