Refactor GetTimeUnitAndMultiplier and add example
diff --git a/README.md b/README.md
index 21ae478..b4c6f7c 100644
--- a/README.md
+++ b/README.md
@@ -190,6 +190,14 @@
}
```
+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 251fd59..f744015 100644
--- a/include/benchmark/benchmark_api.h
+++ b/include/benchmark/benchmark_api.h
@@ -137,6 +137,13 @@
}
}
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 9c4a69d..aaf5fbf 100644
--- a/include/benchmark/reporter.h
+++ b/include/benchmark/reporter.h
@@ -22,6 +22,8 @@
namespace benchmark {
+typedef std::pair<const char*,double> 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 @@
virtual ~BenchmarkReporter();
protected:
- static void ComputeStats(std::vector<Run> const& reports, Run* mean, Run* stddev);
+ static void ComputeStats(std::vector<Run> const& reports, Run* mean, Run* stddev);
+ static TimeUnitMultiplier GetTimeUnitAndMultiplier(TimeUnit unit);
};
-typedef std::pair<const char*,double> 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 @@
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 c07ed5a..375e861 100644
--- a/src/console_reporter.cc
+++ b/src/console_reporter.cc
@@ -95,7 +95,7 @@
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 @@
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 4b47e3d..036546e 100644
--- a/src/reporter.cc
+++ b/src/reporter.cc
@@ -77,6 +77,18 @@
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() {
}