using factory function to make benchmark target.
there are several default factory functions:
bp::real_time: real time benchmarkbp::proc_time: process time benchmark, including user time and system time
auto target = bp::real_time(bench_func, pre_func, post_func);
using bp::bench function to benchmark.
rounds is the number of times to run the benchmark target.
d_meth is the method to use to calculate result. see Result
for multiple targets, the execution order will be shuffled randomly.
bp::bench(rounds, d_meth, target1, target2, ...);
d_meth is used to calculate result.
there are serveral default methods:
bp::avg: average of all roundsbp::min: minimum of all roundsbp::max: maximum of all roundsmin_max: minimum and maximum of all roundsbp::median: median of all roundsbp::stddev: standard deviation of all roundsbp::sum: sum of all roundsbp::avg_stddev: average and standard deviation of all roundsbp::excl_avg: average of all rounds excluding outliersbp::full: all results
bp::bench returns array of result if there is multiple target.
result type is return type of d_meth.
using std::bind to make factory function.
pre_func and post_func can be omitted.
// T_val is type return by clock_func
auto factory(std::function<T(void)> func) {
return std::bind(time<T_val>, clock_func, clock_diff_func, func, pre_func,
post_func);
}
the calculate function should take std::span<std::chrono::nanoseconds>
for example, you want top 3 fastest results
using nanos = std::chrono::nanoseconds;
std::array<nanos> top3(const std::span<nanos> span) {
if(span.size() < 3) {
throw std::runtime_error("not enough results");
}
std::nth_element(span.begin(), span.begin() + 3, span.end());
std::array<nanos, 3> top3{span[0], span[1], span[2]};
return top3;
}