Skip to content

Commit e98c4fd

Browse files
committed
feat: add fibonacci benchmark
1 parent 8718b17 commit e98c4fd

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../google_benchmark_cmake/fibonacci_bench.hpp
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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);

examples/google_benchmark_cmake/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstring>
44

5+
#include "fibonacci_bench.hpp"
56
#include "fixture_bench.hpp"
67
#include "sleep_bench.hpp"
78
#include "template_bench.hpp"

0 commit comments

Comments
 (0)