mirror of
https://github.com/google/benchmark.git
synced 2024-12-27 13:00:36 +08:00
bd721f9859
* Removing warnings appearing with C++20 / CLang 15 ``` [ 70%] Building CXX object _deps/benchmark-build/test/CMakeFiles/benchmark_min_time_flag_time_test.dir/benchmark_min_time_flag_time_test.cc.o /home/xxx/cpp/_deps/benchmark-src/test/benchmark_min_time_flag_time_test.cc:31:55: warning: unused parameter 'has_explicit_iters' [-Wunused-parameter] virtual void ReportRunsConfig(double min_time, bool has_explicit_iters, ^ /home/xxx/cpp/_deps/benchmark-src/test/benchmark_min_time_flag_time_test.cc:32:48: warning: unused parameter 'iters' [-Wunused-parameter] IterationCount iters) BENCHMARK_OVERRIDE { ^ 2 warnings generated. ``` ``` [ 70%] Building CXX object _deps/benchmark-build/test/CMakeFiles/benchmark_min_time_flag_iters_test.dir/benchmark_min_time_flag_iters_test.cc.o /home/xxx/cpp/_deps/benchmark-src/test/benchmark_min_time_flag_iters_test.cc:22:36: warning: implicit conversion loses integer precision: 'const benchmark::IterationCount' (aka 'const long') to 'std::vector<int>::value_type' (aka 'int') [-Wshorten-64-to-32] iter_nums_.push_back(report[0].iterations); ~~~~~~~~~ ~~~~~~~~~~^~~~~~~~~~ 1 warning generated. ``` * Refactoring to get the proper type of collection * Refactoring to get the proper type of collection * clang format * bug fix in main
67 lines
1.7 KiB
C++
67 lines
1.7 KiB
C++
#include <cassert>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
// Tests that we can specify the number of iterations with
|
|
// --benchmark_min_time=<NUM>x.
|
|
namespace {
|
|
|
|
class TestReporter : public benchmark::ConsoleReporter {
|
|
public:
|
|
virtual bool ReportContext(const Context& context) BENCHMARK_OVERRIDE {
|
|
return ConsoleReporter::ReportContext(context);
|
|
};
|
|
|
|
virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
|
|
assert(report.size() == 1);
|
|
iter_nums_.push_back(report[0].iterations);
|
|
ConsoleReporter::ReportRuns(report);
|
|
};
|
|
|
|
TestReporter() {}
|
|
|
|
virtual ~TestReporter() {}
|
|
|
|
const std::vector<benchmark::IterationCount>& GetIters() const {
|
|
return iter_nums_;
|
|
}
|
|
|
|
private:
|
|
std::vector<benchmark::IterationCount> iter_nums_;
|
|
};
|
|
|
|
} // end namespace
|
|
|
|
static void BM_MyBench(benchmark::State& state) {
|
|
for (auto s : state) {
|
|
}
|
|
}
|
|
BENCHMARK(BM_MyBench);
|
|
|
|
int main(int argc, char** argv) {
|
|
// Make a fake argv and append the new --benchmark_min_time=<foo> to it.
|
|
int fake_argc = argc + 1;
|
|
const char** fake_argv = new const char*[fake_argc];
|
|
for (int i = 0; i < argc; ++i) fake_argv[i] = argv[i];
|
|
fake_argv[argc] = "--benchmark_min_time=4x";
|
|
|
|
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv));
|
|
|
|
TestReporter test_reporter;
|
|
const size_t returned_count =
|
|
benchmark::RunSpecifiedBenchmarks(&test_reporter, "BM_MyBench");
|
|
assert(returned_count == 1);
|
|
|
|
// Check the executed iters.
|
|
const std::vector<benchmark::IterationCount> iters = test_reporter.GetIters();
|
|
assert(!iters.empty() && iters[0] == 4);
|
|
|
|
delete[] fake_argv;
|
|
return 0;
|
|
}
|