DGtal  1.3.beta
testBenchmark.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
32 #include <string>
33 #include <sstream>
34 #include <benchmark/benchmark.h>
36 
37 using 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 
51 int BENCHMARK_NOINLINE Factorial(uint32_t n) {
52  return (n == 1) ? 1 : n * Factorial(n - 1);
53 }
54 
55 static 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
65 static void BM_StringCopy(benchmark::State& state)
66 {
67  std::string x = "hello";
68  while (state.KeepRunning())
69  std::string copy(x);
70 }
72 
73 
74 static 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 
85 double 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 
95 static 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 }
103 BENCHMARK_RANGE(BM_CalculatePiRange, 1, 256 * 256);
104 
105 static 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 }
112 BENCHMARK(BM_CalculatePi)->Threads(8);
113 BENCHMARK(BM_CalculatePi)->ThreadRange(1, 16);
114 BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
115 
116 
117 static 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 }
123 BENCHMARK(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'
133 int main(int argc, char* argv[])
134 {
135  benchmark::Initialize(&argc, argv);
136 
137  benchmark::RunSpecifiedBenchmarks();
138  return 0;
139 }// //
BENCHMARK_RANGE
BENCHMARK_RANGE(BM_CalculatePiRange, 1, 256 *256)
main
int main(int argc, char *argv[])
Definition: testBenchmark.cpp:133
BM_LongTest
static void BM_LongTest(benchmark::State &state)
Definition: testBenchmark.cpp:117
BM_Factorial
static void BM_Factorial(benchmark::State &state)
Definition: testBenchmark.cpp:74
BENCHMARK_NOINLINE
#define BENCHMARK_NOINLINE
Definition: testBenchmark.cpp:47
BM_StringCreation
static void BM_StringCreation(benchmark::State &state)
Definition: testBenchmark.cpp:55
BM_StringCopy
static void BM_StringCopy(benchmark::State &state)
Definition: testBenchmark.cpp:65
CalculatePi
double CalculatePi(int depth)
Definition: testBenchmark.cpp:85
BM_CalculatePiRange
static void BM_CalculatePiRange(benchmark::State &state)
Definition: testBenchmark.cpp:95
Factorial
int BENCHMARK_NOINLINE Factorial(uint32_t n)
Definition: testBenchmark.cpp:51
BM_CalculatePi
static void BM_CalculatePi(benchmark::State &state)
Definition: testBenchmark.cpp:105
BENCHMARK
BENCHMARK(BM_StringCreation)