• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "benchmark/benchmark.h"
2 
3 #include <assert.h>
4 #include <math.h>
5 #include <stdint.h>
6 
7 #include <cstdlib>
8 #include <iostream>
9 #include <limits>
10 #include <list>
11 #include <map>
12 #include <mutex>
13 #include <set>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 #if defined(__GNUC__)
19 # define BENCHMARK_NOINLINE __attribute__((noinline))
20 #else
21 # define BENCHMARK_NOINLINE
22 #endif
23 
24 namespace {
25 
Factorial(uint32_t n)26 int BENCHMARK_NOINLINE Factorial(uint32_t n) {
27   return (n == 1) ? 1 : n * Factorial(n - 1);
28 }
29 
CalculatePi(int depth)30 double CalculatePi(int depth) {
31   double pi = 0.0;
32   for (int i = 0; i < depth; ++i) {
33     double numerator = static_cast<double>(((i % 2) * 2) - 1);
34     double denominator = static_cast<double>((2 * i) - 1);
35     pi += numerator / denominator;
36   }
37   return (pi - 1.0) * 4;
38 }
39 
ConstructRandomSet(int size)40 std::set<int> ConstructRandomSet(int size) {
41   std::set<int> s;
42   for (int i = 0; i < size; ++i)
43     s.insert(i);
44   return s;
45 }
46 
47 std::mutex test_vector_mu;
48 std::vector<int>* test_vector = nullptr;
49 
50 }  // end namespace
51 
BM_Factorial(benchmark::State & state)52 static void BM_Factorial(benchmark::State& state) {
53   int fac_42 = 0;
54   while (state.KeepRunning())
55     fac_42 = Factorial(8);
56   // Prevent compiler optimizations
57   std::stringstream ss;
58   ss << fac_42;
59   state.SetLabel(ss.str());
60 }
61 BENCHMARK(BM_Factorial);
62 BENCHMARK(BM_Factorial)->UseRealTime();
63 
BM_CalculatePiRange(benchmark::State & state)64 static void BM_CalculatePiRange(benchmark::State& state) {
65   double pi = 0.0;
66   while (state.KeepRunning())
67     pi = CalculatePi(state.range_x());
68   std::stringstream ss;
69   ss << pi;
70   state.SetLabel(ss.str());
71 }
72 BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
73 
BM_CalculatePi(benchmark::State & state)74 static void BM_CalculatePi(benchmark::State& state) {
75   static const int depth = 1024;
76   while (state.KeepRunning()) {
77     benchmark::DoNotOptimize(CalculatePi(depth));
78   }
79 }
80 BENCHMARK(BM_CalculatePi)->Threads(8);
81 BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
82 BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
83 
BM_SetInsert(benchmark::State & state)84 static void BM_SetInsert(benchmark::State& state) {
85   while (state.KeepRunning()) {
86     state.PauseTiming();
87     std::set<int> data = ConstructRandomSet(state.range_x());
88     state.ResumeTiming();
89     for (int j = 0; j < state.range_y(); ++j)
90       data.insert(rand());
91   }
92   state.SetItemsProcessed(state.iterations() * state.range_y());
93   state.SetBytesProcessed(state.iterations() * state.range_y() * sizeof(int));
94 }
95 BENCHMARK(BM_SetInsert)->RangePair(1<<10,8<<10, 1,10);
96 
97 template<typename Container, typename ValueType = typename Container::value_type>
BM_Sequential(benchmark::State & state)98 static void BM_Sequential(benchmark::State& state) {
99   ValueType v = 42;
100   while (state.KeepRunning()) {
101     Container c;
102     for (int i = state.range_x(); --i; )
103       c.push_back(v);
104   }
105   const size_t items_processed = state.iterations() * state.range_x();
106   state.SetItemsProcessed(items_processed);
107   state.SetBytesProcessed(items_processed * sizeof(v));
108 }
109 BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)->Range(1 << 0, 1 << 10);
110 BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
111 // Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
112 #if __cplusplus >= 201103L
113 BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
114 #endif
115 
BM_StringCompare(benchmark::State & state)116 static void BM_StringCompare(benchmark::State& state) {
117   std::string s1(state.range_x(), '-');
118   std::string s2(state.range_x(), '-');
119   while (state.KeepRunning())
120     benchmark::DoNotOptimize(s1.compare(s2));
121 }
122 BENCHMARK(BM_StringCompare)->Range(1, 1<<20);
123 
BM_SetupTeardown(benchmark::State & state)124 static void BM_SetupTeardown(benchmark::State& state) {
125   if (state.thread_index == 0) {
126     // No need to lock test_vector_mu here as this is running single-threaded.
127     test_vector = new std::vector<int>();
128   }
129   int i = 0;
130   while (state.KeepRunning()) {
131     std::lock_guard<std::mutex> l(test_vector_mu);
132     if (i%2 == 0)
133       test_vector->push_back(i);
134     else
135       test_vector->pop_back();
136     ++i;
137   }
138   if (state.thread_index == 0) {
139     delete test_vector;
140   }
141 }
142 BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
143 
BM_LongTest(benchmark::State & state)144 static void BM_LongTest(benchmark::State& state) {
145   double tracker = 0.0;
146   while (state.KeepRunning()) {
147     for (int i = 0; i < state.range_x(); ++i)
148       benchmark::DoNotOptimize(tracker += i);
149   }
150 }
151 BENCHMARK(BM_LongTest)->Range(1<<16,1<<28);
152 
BM_ParallelMemset(benchmark::State & state)153 static void BM_ParallelMemset(benchmark::State& state) {
154   int size = state.range_x() / sizeof(int);
155   int thread_size = size / state.threads;
156   int from = thread_size * state.thread_index;
157   int to = from + thread_size;
158 
159   if (state.thread_index == 0) {
160     test_vector = new std::vector<int>(size);
161   }
162 
163   while (state.KeepRunning()) {
164     for (int i = from; i < to; i++) {
165       // No need to lock test_vector_mu as ranges
166       // do not overlap between threads.
167       benchmark::DoNotOptimize(test_vector->at(i) = 1);
168     }
169   }
170 
171   if (state.thread_index == 0) {
172     delete test_vector;
173   }
174 }
175 BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);
176 
177 BENCHMARK_MAIN()
178 
179