1 #include "benchmark/benchmark.h" 2 3 #include <assert.h> 4 #include <math.h> 5 #include <stdint.h> 6 7 #include <chrono> 8 #include <cstdlib> 9 #include <iostream> 10 #include <limits> 11 #include <list> 12 #include <map> 13 #include <mutex> 14 #include <set> 15 #include <sstream> 16 #include <string> 17 #include <thread> 18 #include <utility> 19 #include <vector> 20 21 #if defined(__GNUC__) 22 #define BENCHMARK_NOINLINE __attribute__((noinline)) 23 #else 24 #define BENCHMARK_NOINLINE 25 #endif 26 27 namespace { 28 29 int BENCHMARK_NOINLINE Factorial(uint32_t n) { 30 return (n == 1) ? 1 : n * Factorial(n - 1); 31 } 32 33 double CalculatePi(int depth) { 34 double pi = 0.0; 35 for (int i = 0; i < depth; ++i) { 36 double numerator = static_cast<double>(((i % 2) * 2) - 1); 37 double denominator = static_cast<double>((2 * i) - 1); 38 pi += numerator / denominator; 39 } 40 return (pi - 1.0) * 4; 41 } 42 43 std::set<int> ConstructRandomSet(int size) { 44 std::set<int> s; 45 for (int i = 0; i < size; ++i) s.insert(s.end(), i); 46 return s; 47 } 48 49 std::mutex test_vector_mu; 50 std::vector<int>* test_vector = nullptr; 51 52 } // end namespace 53 54 static void BM_Factorial(benchmark::State& state) { 55 int fac_42 = 0; 56 for (auto _ : state) fac_42 = Factorial(8); 57 // Prevent compiler optimizations 58 std::stringstream ss; 59 ss << fac_42; 60 state.SetLabel(ss.str()); 61 } 62 BENCHMARK(BM_Factorial); 63 BENCHMARK(BM_Factorial)->UseRealTime(); 64 65 static void BM_CalculatePiRange(benchmark::State& state) { 66 double pi = 0.0; 67 for (auto _ : state) pi = CalculatePi(state.range(0)); 68 std::stringstream ss; 69 ss << pi; 70 state.SetLabel(ss.str()); 71 } 72 BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024); 73 74 static void BM_CalculatePi(benchmark::State& state) { 75 static const int depth = 1024; 76 for (auto _ : state) { 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 84 static void BM_SetInsert(benchmark::State& state) { 85 std::set<int> data; 86 for (auto _ : state) { 87 state.PauseTiming(); 88 data = ConstructRandomSet(state.range(0)); 89 state.ResumeTiming(); 90 for (int j = 0; j < state.range(1); ++j) data.insert(rand()); 91 } 92 state.SetItemsProcessed(state.iterations() * state.range(1)); 93 state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int)); 94 } 95 96 // Test many inserts at once to reduce the total iterations needed. Otherwise, the slower, 97 // non-timed part of each iteration will make the benchmark take forever. 98 BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}}); 99 100 template <typename Container, 101 typename ValueType = typename Container::value_type> 102 static void BM_Sequential(benchmark::State& state) { 103 ValueType v = 42; 104 for (auto _ : state) { 105 Container c; 106 for (int i = state.range(0); --i;) c.push_back(v); 107 } 108 const size_t items_processed = state.iterations() * state.range(0); 109 state.SetItemsProcessed(items_processed); 110 state.SetBytesProcessed(items_processed * sizeof(v)); 111 } 112 BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int) 113 ->Range(1 << 0, 1 << 10); 114 BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10); 115 // Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond. 116 #ifdef BENCHMARK_HAS_CXX11 117 BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512); 118 #endif 119 120 static void BM_StringCompare(benchmark::State& state) { 121 std::string s1(state.range(0), '-'); 122 std::string s2(state.range(0), '-'); 123 for (auto _ : state) benchmark::DoNotOptimize(s1.compare(s2)); 124 } 125 BENCHMARK(BM_StringCompare)->Range(1, 1 << 20); 126 127 static void BM_SetupTeardown(benchmark::State& state) { 128 if (state.thread_index == 0) { 129 // No need to lock test_vector_mu here as this is running single-threaded. 130 test_vector = new std::vector<int>(); 131 } 132 int i = 0; 133 for (auto _ : state) { 134 std::lock_guard<std::mutex> l(test_vector_mu); 135 if (i % 2 == 0) 136 test_vector->push_back(i); 137 else 138 test_vector->pop_back(); 139 ++i; 140 } 141 if (state.thread_index == 0) { 142 delete test_vector; 143 } 144 } 145 BENCHMARK(BM_SetupTeardown)->ThreadPerCpu(); 146 147 static void BM_LongTest(benchmark::State& state) { 148 double tracker = 0.0; 149 for (auto _ : state) { 150 for (int i = 0; i < state.range(0); ++i) 151 benchmark::DoNotOptimize(tracker += i); 152 } 153 } 154 BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28); 155 156 static void BM_ParallelMemset(benchmark::State& state) { 157 int size = state.range(0) / static_cast<int>(sizeof(int)); 158 int thread_size = size / state.threads; 159 int from = thread_size * state.thread_index; 160 int to = from + thread_size; 161 162 if (state.thread_index == 0) { 163 test_vector = new std::vector<int>(size); 164 } 165 166 for (auto _ : state) { 167 for (int i = from; i < to; i++) { 168 // No need to lock test_vector_mu as ranges 169 // do not overlap between threads. 170 benchmark::DoNotOptimize(test_vector->at(i) = 1); 171 } 172 } 173 174 if (state.thread_index == 0) { 175 delete test_vector; 176 } 177 } 178 BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4); 179 180 static void BM_ManualTiming(benchmark::State& state) { 181 size_t slept_for = 0; 182 int microseconds = state.range(0); 183 std::chrono::duration<double, std::micro> sleep_duration{ 184 static_cast<double>(microseconds)}; 185 186 for (auto _ : state) { 187 auto start = std::chrono::high_resolution_clock::now(); 188 // Simulate some useful workload with a sleep 189 std::this_thread::sleep_for( 190 std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration)); 191 auto end = std::chrono::high_resolution_clock::now(); 192 193 auto elapsed = 194 std::chrono::duration_cast<std::chrono::duration<double>>(end - start); 195 196 state.SetIterationTime(elapsed.count()); 197 slept_for += microseconds; 198 } 199 state.SetItemsProcessed(slept_for); 200 } 201 BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime(); 202 BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime(); 203 204 #ifdef BENCHMARK_HAS_CXX11 205 206 template <class... Args> 207 void BM_with_args(benchmark::State& state, Args&&...) { 208 for (auto _ : state) { 209 } 210 } 211 BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44); 212 BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test, std::string("abc"), 213 std::pair<int, double>(42, 3.8)); 214 215 void BM_non_template_args(benchmark::State& state, int, double) { 216 while(state.KeepRunning()) {} 217 } 218 BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0); 219 220 #endif // BENCHMARK_HAS_CXX11 221 222 static void BM_DenseThreadRanges(benchmark::State& st) { 223 switch (st.range(0)) { 224 case 1: 225 assert(st.threads == 1 || st.threads == 2 || st.threads == 3); 226 break; 227 case 2: 228 assert(st.threads == 1 || st.threads == 3 || st.threads == 4); 229 break; 230 case 3: 231 assert(st.threads == 5 || st.threads == 8 || st.threads == 11 || 232 st.threads == 14); 233 break; 234 default: 235 assert(false && "Invalid test case number"); 236 } 237 while (st.KeepRunning()) { 238 } 239 } 240 BENCHMARK(BM_DenseThreadRanges)->Arg(1)->DenseThreadRange(1, 3); 241 BENCHMARK(BM_DenseThreadRanges)->Arg(2)->DenseThreadRange(1, 4, 2); 242 BENCHMARK(BM_DenseThreadRanges)->Arg(3)->DenseThreadRange(5, 14, 3); 243 244 BENCHMARK_MAIN(); 245