Micro benchmarking class.
- Used to benchmark related blocks of code for comparison
- This can benchmark a function or functor (object with
operator()()
– note that this method must be const)
- Functors are useful for "parameterizing" the code being benchmarked (via methods/properties)
- Each benchmark runs a given function/functor repeatedly according to a repeat count and measures time elapsed
- Usage:
- Call scale() first to automatically set a default repeat count (optional)
- Call run() for each benchmark to run
- Call report() to show results and clear stored report – the destructor calls this if there's pending report data
- Report includes a "DiffBest" column showing the difference between AvgCPU of current call and the best (fastest) call in this report – lower value is better
- Repeat if needed
Shortcut helpers:
- Example
inline void bm_function() {
for (ulong i = 0; i < 100; ++i)
printf("%s", "");
}
struct BM_Class {
void operator()() const {
for (ulong i = 0; i < 50; ++i)
printf("%s", "");
}
};
int main() {
bench.
scale(bm_function);
bench.
run(
"bm_function", bm_function);
bench.
run(
"BM_Class", BM_Class());
return 0;
}
Example output:
Name Time(nsec) CPU(nsec) Count AvgTime(nsec) AvgCPU(nsec) DiffBest(nsec)
bm_function 204529700 203125000 1000000 204.5297 203.125 109.375
BM_Class 96745700 93750000 1000000 96.7457 93.75 0
This is the same benchmarking code using the shortcut macros:
- Parameterized Example
Here's an example that benchmarks parameterized code using a functor:
struct BM_Class {
ulong count;
BM_Class() : count(50) {
}
void get_name(
String& name)
const {
name << '/' << count;
}
void operator()() const {
for (ulong i = 0; i < count; ++i)
printf("%s", "");
}
};
int main() {
BM_Class bm_obj;
bm_obj.count = 60;
return 0;
}
Example output:
Name Time(nsec) CPU(nsec) Count AvgTime(nsec) AvgCPU(nsec) DiffBest(nsec)
bm_obj/50 83509600 78125000 1000000 83.5096 78.125 0
bm_obj/60 112968900 109375000 1000000 112.9689 109.375 31.25