File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ ../google_benchmark_cmake/pause_timing_bench.hpp
Original file line number Diff line number Diff line change 55#include " fibonacci_bench.hpp"
66#include " fixture_bench.hpp"
77#include " multithread_bench.hpp"
8+ #include " pause_timing_bench.hpp"
89#include " sleep_bench.hpp"
910#include " template_bench.hpp"
1011
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < benchmark/benchmark.h>
4+
5+ #include < chrono>
6+ #include < numeric>
7+ #include < thread>
8+ #include < vector>
9+
10+ __attribute__ ((noinline)) static uint64_t recursive_fib(int n) {
11+ if (n <= 1 ) return n;
12+ return recursive_fib (n - 1 ) + recursive_fib (n - 2 );
13+ }
14+
15+ __attribute__ ((noinline)) static uint64_t expensive_operation() {
16+ // Large memory allocation
17+ std::vector<uint64_t > data (1024 * 1024 , 42 ); // 8 MiB allocation
18+
19+ // Expensive recursive computation that will dominate flamegraph
20+ uint64_t fib_result = recursive_fib (30 );
21+
22+ // More expensive work
23+ uint64_t sum = std::accumulate (data.begin (), data.end (), uint64_t (0 ));
24+ benchmark::DoNotOptimize (sum);
25+ benchmark::DoNotOptimize (fib_result);
26+
27+ return sum + fib_result;
28+ }
29+
30+ NOINLINE static uint64_t actual_work () {
31+ std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
32+ return 42 ;
33+ }
34+
35+ static void BM_large_drop (benchmark::State& state) {
36+ for (auto _ : state) {
37+ state.PauseTiming ();
38+ benchmark::DoNotOptimize (expensive_operation ());
39+ state.ResumeTiming ();
40+
41+ benchmark::DoNotOptimize (actual_work ());
42+
43+ state.PauseTiming ();
44+ benchmark::DoNotOptimize (expensive_operation ());
45+ state.ResumeTiming ();
46+ }
47+ }
48+ BENCHMARK (BM_large_drop);
49+
50+ static void BM_large_setup (benchmark::State& state) {
51+ for (auto _ : state) {
52+ state.PauseTiming ();
53+ benchmark::DoNotOptimize (expensive_operation ());
54+ state.ResumeTiming ();
55+
56+ benchmark::DoNotOptimize (actual_work ());
57+ }
58+ }
59+ BENCHMARK (BM_large_setup);
You can’t perform that action at this time.
0 commit comments