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