diff --git a/README.md b/README.md index 21ae478b..b4c6f7c2 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,14 @@ static void BM_test(benchmark::State& state) { } ``` +If a benchmark runs a few milliseconds it may be hard to visually compare the +measured times, since the output data is given in nanoseconds per default. In +order to manually set the time unit, you can specify it manually: + +```c++ +BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); +``` + Benchmark Fixtures ------------------ Fixture tests are created by diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h index 251fd59d..f7440151 100644 --- a/include/benchmark/benchmark_api.h +++ b/include/benchmark/benchmark_api.h @@ -137,6 +137,13 @@ static void BM_MultiThreaded(benchmark::State& state) { } } BENCHMARK(BM_MultiThreaded)->Threads(4); + + +If a benchmark runs a few milliseconds it may be hard to visually compare the +measured times, since the output data is given in nanoseconds per default. In +order to manually set the time unit, you can specify it manually: + +BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); */ #ifndef BENCHMARK_BENCHMARK_API_H_ diff --git a/include/benchmark/reporter.h b/include/benchmark/reporter.h index 9c4a69d3..aaf5fbff 100644 --- a/include/benchmark/reporter.h +++ b/include/benchmark/reporter.h @@ -22,6 +22,8 @@ namespace benchmark { +typedef std::pair TimeUnitMultiplier; + // Interface for custom benchmark result printers. // By default, benchmark reports are printed to stdout. However an application // can control the destination of the reports by calling @@ -83,11 +85,10 @@ class BenchmarkReporter { virtual ~BenchmarkReporter(); protected: - static void ComputeStats(std::vector const& reports, Run* mean, Run* stddev); + static void ComputeStats(std::vector const& reports, Run* mean, Run* stddev); + static TimeUnitMultiplier GetTimeUnitAndMultiplier(TimeUnit unit); }; -typedef std::pair TimeUnitMultiplier; - // Simple reporter that outputs benchmark data to the console. This is the // default reporter used by RunSpecifiedBenchmarks(). class ConsoleReporter : public BenchmarkReporter { @@ -98,9 +99,6 @@ class ConsoleReporter : public BenchmarkReporter { protected: virtual void PrintRunData(const Run& report); - private: - TimeUnitMultiplier getTimeUnitAndMultiplier(TimeUnit unit); - size_t name_field_width_; }; diff --git a/src/console_reporter.cc b/src/console_reporter.cc index c07ed5ae..375e8610 100644 --- a/src/console_reporter.cc +++ b/src/console_reporter.cc @@ -95,7 +95,7 @@ void ConsoleReporter::PrintRunData(const Run& result) { double multiplier; const char* timeLabel; - std::tie(timeLabel, multiplier) = getTimeUnitAndMultiplier(result.time_unit); + std::tie(timeLabel, multiplier) = GetTimeUnitAndMultiplier(result.time_unit); ColorPrintf(COLOR_GREEN, "%-*s ", name_field_width_, result.benchmark_name.c_str()); @@ -121,16 +121,4 @@ void ConsoleReporter::PrintRunData(const Run& result) { result.report_label.c_str()); } -TimeUnitMultiplier ConsoleReporter::getTimeUnitAndMultiplier(TimeUnit unit) { - switch (unit) { - case kMillisecond: - return std::make_pair("ms", 1e3); - case kMicrosecond: - return std::make_pair("us", 1e6); - case kNanosecond: - default: - return std::make_pair("ns", 1e9); - } -} - } // end namespace benchmark diff --git a/src/reporter.cc b/src/reporter.cc index 4b47e3d5..036546e7 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -77,6 +77,18 @@ void BenchmarkReporter::ComputeStats( stddev_data->items_per_second = items_per_second_stat.StdDev(); } +TimeUnitMultiplier BenchmarkReporter::GetTimeUnitAndMultiplier(TimeUnit unit) { + switch (unit) { + case kMillisecond: + return std::make_pair("ms", 1e3); + case kMicrosecond: + return std::make_pair("us", 1e6); + case kNanosecond: + default: + return std::make_pair("ns", 1e9); + } +} + void BenchmarkReporter::Finalize() { }