You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.3 KiB

3 weeks ago
  1. #include <chrono>
  2. #include <thread>
  3. #include "benchmark/benchmark.h"
  4. #if defined(NDEBUG)
  5. #undef NDEBUG
  6. #endif
  7. #include <cassert>
  8. void BM_basic(benchmark::State& state) {
  9. for (auto _ : state) {
  10. }
  11. }
  12. void BM_basic_slow(benchmark::State& state) {
  13. std::chrono::milliseconds sleep_duration(state.range(0));
  14. for (auto _ : state) {
  15. std::this_thread::sleep_for(
  16. std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
  17. }
  18. }
  19. BENCHMARK(BM_basic);
  20. BENCHMARK(BM_basic)->Arg(42);
  21. BENCHMARK(BM_basic_slow)->Arg(10)->Unit(benchmark::kNanosecond);
  22. BENCHMARK(BM_basic_slow)->Arg(100)->Unit(benchmark::kMicrosecond);
  23. BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kMillisecond);
  24. BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kSecond);
  25. BENCHMARK(BM_basic)->Range(1, 8);
  26. BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8);
  27. BENCHMARK(BM_basic)->DenseRange(10, 15);
  28. BENCHMARK(BM_basic)->Args({42, 42});
  29. BENCHMARK(BM_basic)->Ranges({{64, 512}, {64, 512}});
  30. BENCHMARK(BM_basic)->MinTime(0.7);
  31. BENCHMARK(BM_basic)->MinWarmUpTime(0.8);
  32. BENCHMARK(BM_basic)->MinTime(0.1)->MinWarmUpTime(0.2);
  33. BENCHMARK(BM_basic)->UseRealTime();
  34. BENCHMARK(BM_basic)->ThreadRange(2, 4);
  35. BENCHMARK(BM_basic)->ThreadPerCpu();
  36. BENCHMARK(BM_basic)->Repetitions(3);
  37. BENCHMARK(BM_basic)
  38. ->RangeMultiplier(std::numeric_limits<int>::max())
  39. ->Range(std::numeric_limits<int64_t>::min(),
  40. std::numeric_limits<int64_t>::max());
  41. // Negative ranges
  42. BENCHMARK(BM_basic)->Range(-64, -1);
  43. BENCHMARK(BM_basic)->RangeMultiplier(4)->Range(-8, 8);
  44. BENCHMARK(BM_basic)->DenseRange(-2, 2, 1);
  45. BENCHMARK(BM_basic)->Ranges({{-64, 1}, {-8, -1}});
  46. void CustomArgs(benchmark::internal::Benchmark* b) {
  47. for (int i = 0; i < 10; ++i) {
  48. b->Arg(i);
  49. }
  50. }
  51. BENCHMARK(BM_basic)->Apply(CustomArgs);
  52. void BM_explicit_iteration_count(benchmark::State& state) {
  53. // Test that benchmarks specified with an explicit iteration count are
  54. // only run once.
  55. static bool invoked_before = false;
  56. assert(!invoked_before);
  57. invoked_before = true;
  58. // Test that the requested iteration count is respected.
  59. assert(state.max_iterations == 42);
  60. for (auto _ : state) {
  61. }
  62. assert(state.iterations() == state.max_iterations);
  63. assert(state.iterations() == 42);
  64. }
  65. BENCHMARK(BM_explicit_iteration_count)->Iterations(42);
  66. BENCHMARK_MAIN();