DGtal 1.3.0
Loading...
Searching...
No Matches
testBenchmark.cpp
Go to the documentation of this file.
1
31#include <iostream>
32#include <string>
33#include <sstream>
34#include <benchmark/benchmark.h>
36
37using namespace std;
38
40// Functions for testing class Benchmark.
42
43
44#if defined(__GNUC__)
45# define BENCHMARK_NOINLINE __attribute__((noinline))
46#else
47# define BENCHMARK_NOINLINE
48#endif
49
50
52 return (n == 1) ? 1 : n * Factorial(n - 1);
53}
54
55static void BM_StringCreation(benchmark::State& state)
56{
57 while (state.KeepRunning())
58 std::string empty_string;
59}
60// Register the function as a benchmark
62
63
64// Define another benchmark
65static void BM_StringCopy(benchmark::State& state)
66{
67 std::string x = "hello";
68 while (state.KeepRunning())
69 std::string copy(x);
70}
72
73
74static void BM_Factorial(benchmark::State& state) {
75 int fac_42 = 0;
76 while (state.KeepRunning())
77 fac_42 = Factorial(8);
78 // Prevent compiler optimizations
79 std::stringstream ss;
80 ss << fac_42;
81 state.SetLabel(ss.str());
82}
84
85double CalculatePi(int depth) {
86 double pi = 0.0;
87 for (int i = 0; i < depth; ++i) {
88 double numerator = static_cast<double>(((i % 2) * 2) - 1);
89 double denominator = static_cast<double>((2 * i) - 1);
90 pi += numerator / denominator;
91 }
92 return (pi - 1.0) * 4;
93}
94
95static void BM_CalculatePiRange(benchmark::State& state) {
96 double pi = 0.0;
97 while (state.KeepRunning())
98 pi = CalculatePi(state.range(0));
99 std::stringstream ss;
100 ss << pi;
101 state.SetLabel(ss.str());
102}
104
105static void BM_CalculatePi(benchmark::State& state) {
106 static const int depth = 256;
107 double pi = 0.0;
108 while (state.KeepRunning()) {
109 benchmark::DoNotOptimize( pi = CalculatePi(depth) );
110 }
111}
113BENCHMARK(BM_CalculatePi)->ThreadRange(1, 16);
114BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
115
116
117static void BM_LongTest(benchmark::State& state) {
118 double tracker = 0.0;
119 while (state.KeepRunning())
120 for (int i = 0; i < state.range(0); ++i)
121 benchmark::DoNotOptimize(tracker += i);
122}
123BENCHMARK(BM_LongTest)->Range(1<<4,1<<6);
124
125
126
127// Augment the main() program to invoke benchmarks if specified
128// via the --benchmarks command line flag. E.g.,
129// testBenchmark --benchmark_filter=all
130// testBenchmark --benchmark_filter=BM_StringCreation
131// testBenchmark --benchmark_filter=String
132// testBenchmark --benchmark_filter='Copy|Creation'
133int main(int argc, char* argv[])
134{
135 benchmark::Initialize(&argc, argv);
136
137 benchmark::RunSpecifiedBenchmarks();
138 return 0;
139}// //
STL namespace.
static void BM_StringCreation(benchmark::State &state)
#define BENCHMARK_NOINLINE
BENCHMARK(BM_StringCreation)
static void BM_CalculatePiRange(benchmark::State &state)
static void BM_CalculatePi(benchmark::State &state)
static void BM_StringCopy(benchmark::State &state)
double CalculatePi(int depth)
static void BM_Factorial(benchmark::State &state)
int BENCHMARK_NOINLINE Factorial(uint32_t n)
static void BM_LongTest(benchmark::State &state)
BENCHMARK_RANGE(BM_CalculatePiRange, 1, 256 *256)
int main()
Definition: testBits.cpp:56