1 /**
2 Example application
3 Copyright: Copyright Nouredine Hussain 2017.
4 Authors: Nouredine Hussain
5 License: $(LINK3 http://www.boost.org/LICENSE_1_0.txt, Boost Software License - Version 1.0).
6 */
7 
8 module app;
9 
10 
11 import std.stdio;
12 import std.json;
13 
14 import simplebench;
15 
16 
17 immutable N = 25;
18 // Function to bench
19 ulong fib_rec(immutable int n){
20   if(n <= 0)
21     return 0;
22   else if(n==1 || n==2)
23     return 1;
24   else
25     return fib_rec(n-1) + fib_rec(n-2);
26 }
27 
28 
29 // Function to bench
30 ulong fib_for_loop(immutable int n) {
31     ulong a=0, b=1;
32     for(int i=0; i < n; ++i) {
33       auto t = b;
34       b = a + b;
35       a = t;
36     }
37     return a;
38   }
39 
40 
41 // The proper test function
42 void test_fib_rec(ref Bencher bencher){
43   int n=N; // Init variables and allocate memory
44   bencher.iter((){
45       return fib_rec(n); // The real code to bench
46   });
47 }
48 
49 
50 
51 void main()
52 {
53   // The test function have to be static
54   static void test_fib_for_loop(ref Bencher bencher){
55     int n=N;
56     bencher.iter((){
57         return fib_for_loop(n);
58     });
59   }
60 
61   assert(fib_for_loop(N) == fib_rec(N));
62   // Run the benchmarks
63   auto br = BenchMain!(test_fib_rec, test_fib_for_loop);
64   // Convert the results to JSON
65   writeln(br.toJSON.toPrettyString);
66 
67 }