/build/arrayfire/src/arrayfire-full-3.6.1/docs/pages/timing.md
Go to the documentation of this file.
1 Timing Your Code {#timing}
2 ================
3 
4 timer() : A platform-independent timer with microsecond accuracy:
5 * [timer::start()](\ref af::timer::start) starts a timer
6 
7 * [timer::start()](\ref af::timer::stop) seconds since last \ref af::timer::start "start"
8 
9 * \ref af::timer::stop(af::timer start) "timer::start(timer start)" seconds since 'start'
10 
11 Example: single timer
12 
13 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
14  // start timer
15  timer::start();
16  // run your code
17  printf("elapsed seconds: %g\n", timer::stop());
18 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 
20 Example: multiple timers
21 
22 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
23  // start timers
24  timer start1 = timer::start();
25  timer start2 = timer::start();
26  // run some code
27  printf("elapsed seconds: %g\n", timer::stop(start1));
28  // run more code
29  printf("elapsed seconds: %g\n", timer::stop(start2));
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 
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.
35 
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.
38 
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.
42 
43 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
44 #include <stdio.h>
45 #include <arrayfire.h>
46 using namespace af;
47 
48 void pi_function() {
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;
53 }
54 
55 int main() {
56  printf("pi_function took %g seconds\n", timeit(pi_function));
57  return 0;
58 }
59 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 
61 This produces:
62 
63  pi_function took 0.007252 seconds
64  (test machine: Core i7 920 @ 2.67GHz with a Tesla C2070)