2016-05-02 17:53:45 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2015-03-27 03:37:26 +00:00
|
|
|
|
2021-11-10 16:22:31 +00:00
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
2017-04-18 04:29:28 +00:00
|
|
|
#if defined(NDEBUG)
|
|
|
|
#undef NDEBUG
|
|
|
|
#endif
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-03-27 03:37:26 +00:00
|
|
|
void BM_basic(benchmark::State& state) {
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2015-03-27 03:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 21:47:27 +00:00
|
|
|
|
|
|
|
void BM_basic_slow(benchmark::State& state) {
|
2016-08-04 19:30:14 +00:00
|
|
|
std::chrono::milliseconds sleep_duration(state.range(0));
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2016-07-22 21:42:26 +00:00
|
|
|
std::this_thread::sleep_for(
|
2016-10-07 18:04:50 +00:00
|
|
|
std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
|
2016-03-25 21:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:37:26 +00:00
|
|
|
BENCHMARK(BM_basic);
|
|
|
|
BENCHMARK(BM_basic)->Arg(42);
|
2016-03-25 21:47:27 +00:00
|
|
|
BENCHMARK(BM_basic_slow)->Arg(10)->Unit(benchmark::kNanosecond);
|
|
|
|
BENCHMARK(BM_basic_slow)->Arg(100)->Unit(benchmark::kMicrosecond);
|
|
|
|
BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kMillisecond);
|
2020-12-21 17:15:58 +00:00
|
|
|
BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kSecond);
|
2015-03-27 03:37:26 +00:00
|
|
|
BENCHMARK(BM_basic)->Range(1, 8);
|
2016-05-14 13:56:34 +00:00
|
|
|
BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8);
|
2015-03-27 03:37:26 +00:00
|
|
|
BENCHMARK(BM_basic)->DenseRange(10, 15);
|
2016-08-04 19:30:14 +00:00
|
|
|
BENCHMARK(BM_basic)->Args({42, 42});
|
|
|
|
BENCHMARK(BM_basic)->Ranges({{64, 512}, {64, 512}});
|
2015-03-27 03:37:26 +00:00
|
|
|
BENCHMARK(BM_basic)->MinTime(0.7);
|
2022-05-23 12:50:17 +00:00
|
|
|
BENCHMARK(BM_basic)->MinWarmUpTime(0.8);
|
|
|
|
BENCHMARK(BM_basic)->MinTime(0.1)->MinWarmUpTime(0.2);
|
2015-03-27 03:37:26 +00:00
|
|
|
BENCHMARK(BM_basic)->UseRealTime();
|
|
|
|
BENCHMARK(BM_basic)->ThreadRange(2, 4);
|
|
|
|
BENCHMARK(BM_basic)->ThreadPerCpu();
|
2016-05-25 03:52:23 +00:00
|
|
|
BENCHMARK(BM_basic)->Repetitions(3);
|
2019-03-26 10:50:53 +00:00
|
|
|
BENCHMARK(BM_basic)
|
|
|
|
->RangeMultiplier(std::numeric_limits<int>::max())
|
|
|
|
->Range(std::numeric_limits<int64_t>::min(),
|
|
|
|
std::numeric_limits<int64_t>::max());
|
|
|
|
|
|
|
|
// Negative ranges
|
|
|
|
BENCHMARK(BM_basic)->Range(-64, -1);
|
|
|
|
BENCHMARK(BM_basic)->RangeMultiplier(4)->Range(-8, 8);
|
|
|
|
BENCHMARK(BM_basic)->DenseRange(-2, 2, 1);
|
|
|
|
BENCHMARK(BM_basic)->Ranges({{-64, 1}, {-8, -1}});
|
2015-03-27 03:37:26 +00:00
|
|
|
|
2015-09-23 18:44:26 +00:00
|
|
|
void CustomArgs(benchmark::internal::Benchmark* b) {
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
b->Arg(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCHMARK(BM_basic)->Apply(CustomArgs);
|
|
|
|
|
2017-10-17 18:17:02 +00:00
|
|
|
void BM_explicit_iteration_count(benchmark::State& state) {
|
2017-04-18 04:29:28 +00:00
|
|
|
// Test that benchmarks specified with an explicit iteration count are
|
|
|
|
// only run once.
|
|
|
|
static bool invoked_before = false;
|
|
|
|
assert(!invoked_before);
|
|
|
|
invoked_before = true;
|
|
|
|
|
|
|
|
// Test that the requested iteration count is respected.
|
2017-10-17 18:17:02 +00:00
|
|
|
assert(state.max_iterations == 42);
|
2022-12-19 14:03:11 +00:00
|
|
|
for (auto _ : state) {
|
|
|
|
}
|
2017-10-17 18:17:02 +00:00
|
|
|
assert(state.iterations() == state.max_iterations);
|
|
|
|
assert(state.iterations() == 42);
|
2017-04-18 04:29:28 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_explicit_iteration_count)->Iterations(42);
|
|
|
|
|
2017-12-04 01:45:07 +00:00
|
|
|
BENCHMARK_MAIN();
|