mirror of
https://github.com/google/benchmark.git
synced 2025-01-27 20:30:15 +08:00
Refactor GetTimeUnitAndMultiplier and add example
This commit is contained in:
parent
7c69b36078
commit
0b4111c3b3
@ -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
|
Benchmark Fixtures
|
||||||
------------------
|
------------------
|
||||||
Fixture tests are created by
|
Fixture tests are created by
|
||||||
|
@ -137,6 +137,13 @@ static void BM_MultiThreaded(benchmark::State& state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
BENCHMARK(BM_MultiThreaded)->Threads(4);
|
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_
|
#ifndef BENCHMARK_BENCHMARK_API_H_
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
namespace benchmark {
|
namespace benchmark {
|
||||||
|
|
||||||
|
typedef std::pair<const char*,double> TimeUnitMultiplier;
|
||||||
|
|
||||||
// Interface for custom benchmark result printers.
|
// Interface for custom benchmark result printers.
|
||||||
// By default, benchmark reports are printed to stdout. However an application
|
// By default, benchmark reports are printed to stdout. However an application
|
||||||
// can control the destination of the reports by calling
|
// can control the destination of the reports by calling
|
||||||
@ -83,11 +85,10 @@ class BenchmarkReporter {
|
|||||||
|
|
||||||
virtual ~BenchmarkReporter();
|
virtual ~BenchmarkReporter();
|
||||||
protected:
|
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
|
// Simple reporter that outputs benchmark data to the console. This is the
|
||||||
// default reporter used by RunSpecifiedBenchmarks().
|
// default reporter used by RunSpecifiedBenchmarks().
|
||||||
class ConsoleReporter : public BenchmarkReporter {
|
class ConsoleReporter : public BenchmarkReporter {
|
||||||
@ -98,9 +99,6 @@ class ConsoleReporter : public BenchmarkReporter {
|
|||||||
protected:
|
protected:
|
||||||
virtual void PrintRunData(const Run& report);
|
virtual void PrintRunData(const Run& report);
|
||||||
|
|
||||||
private:
|
|
||||||
TimeUnitMultiplier getTimeUnitAndMultiplier(TimeUnit unit);
|
|
||||||
|
|
||||||
size_t name_field_width_;
|
size_t name_field_width_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ void ConsoleReporter::PrintRunData(const Run& result) {
|
|||||||
|
|
||||||
double multiplier;
|
double multiplier;
|
||||||
const char* timeLabel;
|
const char* timeLabel;
|
||||||
std::tie(timeLabel, multiplier) = getTimeUnitAndMultiplier(result.time_unit);
|
std::tie(timeLabel, multiplier) = GetTimeUnitAndMultiplier(result.time_unit);
|
||||||
|
|
||||||
ColorPrintf(COLOR_GREEN, "%-*s ",
|
ColorPrintf(COLOR_GREEN, "%-*s ",
|
||||||
name_field_width_, result.benchmark_name.c_str());
|
name_field_width_, result.benchmark_name.c_str());
|
||||||
@ -121,16 +121,4 @@ void ConsoleReporter::PrintRunData(const Run& result) {
|
|||||||
result.report_label.c_str());
|
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
|
} // end namespace benchmark
|
||||||
|
@ -77,6 +77,18 @@ void BenchmarkReporter::ComputeStats(
|
|||||||
stddev_data->items_per_second = items_per_second_stat.StdDev();
|
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() {
|
void BenchmarkReporter::Finalize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user