X20-BenchmarkingMacros.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // X20-BenchmarkingMacros.cpp
  2. // Test that the benchmarking support macros compile properly with the single header
  3. #define CATCH_CONFIG_MAIN
  4. #include <catch2/catch.hpp>
  5. namespace {
  6. std::uint64_t factorial(std::uint64_t number) {
  7. if (number < 2) {
  8. return 1;
  9. }
  10. return number * factorial(number - 1);
  11. }
  12. }
  13. TEST_CASE("Benchmark factorial", "[benchmark]") {
  14. CHECK(factorial(0) == 1);
  15. // some more asserts..
  16. CHECK(factorial(10) == 3628800);
  17. BENCHMARK("factorial 10") {
  18. return factorial(10);
  19. };
  20. CHECK(factorial(14) == 87178291200ull);
  21. BENCHMARK("factorial 14") {
  22. return factorial(14);
  23. };
  24. }
  25. TEST_CASE("Benchmark containers", "[.][benchmark]") {
  26. static const int size = 100;
  27. std::vector<int> v;
  28. std::map<int, int> m;
  29. SECTION("without generator") {
  30. BENCHMARK("Load up a vector") {
  31. v = std::vector<int>();
  32. for (int i = 0; i < size; ++i)
  33. v.push_back(i);
  34. };
  35. REQUIRE(v.size() == size);
  36. // test optimizer control
  37. BENCHMARK("Add up a vector's content") {
  38. uint64_t add = 0;
  39. for (int i = 0; i < size; ++i)
  40. add += v[i];
  41. return add;
  42. };
  43. BENCHMARK("Load up a map") {
  44. m = std::map<int, int>();
  45. for (int i = 0; i < size; ++i)
  46. m.insert({ i, i + 1 });
  47. };
  48. REQUIRE(m.size() == size);
  49. BENCHMARK("Reserved vector") {
  50. v = std::vector<int>();
  51. v.reserve(size);
  52. for (int i = 0; i < size; ++i)
  53. v.push_back(i);
  54. };
  55. REQUIRE(v.size() == size);
  56. BENCHMARK("Resized vector") {
  57. v = std::vector<int>();
  58. v.resize(size);
  59. for (int i = 0; i < size; ++i)
  60. v[i] = i;
  61. };
  62. REQUIRE(v.size() == size);
  63. int array[size];
  64. BENCHMARK("A fixed size array that should require no allocations") {
  65. for (int i = 0; i < size; ++i)
  66. array[i] = i;
  67. };
  68. int sum = 0;
  69. for (int i = 0; i < size; ++i)
  70. sum += array[i];
  71. REQUIRE(sum > size);
  72. SECTION("XYZ") {
  73. BENCHMARK_ADVANCED("Load up vector with chronometer")(Catch::Benchmark::Chronometer meter) {
  74. std::vector<int> k;
  75. meter.measure([&](int idx) {
  76. k = std::vector<int>();
  77. for (int i = 0; i < size; ++i)
  78. k.push_back(idx);
  79. });
  80. REQUIRE(k.size() == size);
  81. };
  82. int runs = 0;
  83. BENCHMARK("Fill vector indexed", benchmarkIndex) {
  84. v = std::vector<int>();
  85. v.resize(size);
  86. for (int i = 0; i < size; ++i)
  87. v[i] = benchmarkIndex;
  88. runs = benchmarkIndex;
  89. };
  90. for (size_t i = 0; i < v.size(); ++i) {
  91. REQUIRE(v[i] == runs);
  92. }
  93. }
  94. }
  95. SECTION("with generator") {
  96. auto generated = GENERATE(range(0, 10));
  97. BENCHMARK("Fill vector generated") {
  98. v = std::vector<int>();
  99. v.resize(size);
  100. for (int i = 0; i < size; ++i)
  101. v[i] = generated;
  102. };
  103. for (size_t i = 0; i < v.size(); ++i) {
  104. REQUIRE(v[i] == generated);
  105. }
  106. }
  107. }