benchmark_test.cc 4.1 KB

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