File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ ../google_benchmark_cmake/fibonacci_bench.hpp
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include < benchmark/benchmark.h>
4
+
5
+ static uint64_t fibonacci_recursive (int n) {
6
+ if (n <= 1 ) return n;
7
+ return fibonacci_recursive (n - 1 ) + fibonacci_recursive (n - 2 );
8
+ }
9
+
10
+ static void BM_FibonacciRecursive (benchmark::State& state) {
11
+ int n = static_cast <int >(state.range (0 ));
12
+ for (auto _ : state) {
13
+ uint64_t result = fibonacci_recursive (n);
14
+ benchmark::DoNotOptimize (result);
15
+ }
16
+ }
17
+ BENCHMARK (BM_FibonacciRecursive)->Arg(35 )->MinTime(5 );
18
+
19
+ static uint64_t fibonacci_iterative (int n) {
20
+ if (n <= 1 ) return n;
21
+
22
+ uint64_t a = 0 , b = 1 ;
23
+ for (int i = 2 ; i <= n; ++i) {
24
+ uint64_t c = a + b;
25
+ a = b;
26
+ b = c;
27
+ }
28
+ return b;
29
+ }
30
+
31
+ static void BM_FibonacciIterative (benchmark::State& state) {
32
+ int n = static_cast <int >(state.range (0 ));
33
+ for (auto _ : state) {
34
+ uint64_t result = fibonacci_iterative (n);
35
+ benchmark::DoNotOptimize (result);
36
+ }
37
+ }
38
+ BENCHMARK (BM_FibonacciIterative)->Arg(50 );
Original file line number Diff line number Diff line change 2
2
3
3
#include < cstring>
4
4
5
+ #include " fibonacci_bench.hpp"
5
6
#include " fixture_bench.hpp"
6
7
#include " sleep_bench.hpp"
7
8
#include " template_bench.hpp"
You can’t perform that action at this time.
0 commit comments