Hardware-based tick counters for high-precision benchmarks
- x86_64: executes RDTSC CPU instruction to read the time-stamp counter.
- AArch64: reads value of the CNTVCT_EL0 counter-timer register.
The breif description: https://sheroz.com/pages/blog/rust-hardware-based-tick-counters-for-high-precision-benchmarks.html
Tested on platforms
x86_64 (Intel® Core™ i7)
AArch64 (Apple M1 Pro)
Usage
For usage samples please look at src/bin/sample.rs
Basic usage
let start = start;
// ... lines of code to benchmark
let elapsed_ticks = stop - start;
println!;
Basic usage with helper
use TickCounter;
let tick_counter = current;
// ... lines of code to benchmark
let elapsed_ticks = tick_counter.elapsed;
println!;
Extended usage
println!;
let = frequency;
let frequency_base = match accuracy ;
println!;
let counter_accuracy = precision_nanoseconds;
println!;
let counter_start = start;
sleep;
let counter_stop = stop;
println!;
println!;
let elapsed_ticks = counter_stop - counter_start;
println!;
let elapsed_nanoseconds = * counter_accuracy;
println!;
Outputs
1. Macbook Pro 16 2021 / Apple Silicon
Apple M1 Pro
MacOS Ventura 13.5.1, Darwin Kernel Version 22.6.0
Output
Basic usage:
Number of elapsed ticks in 1s: 24120997
Basic usage with helper:
Number of elapsed ticks in 1s: 24122097
Extended usage:
Environment: macos/unix aarch64
Tick frequency, MHZ: 24.00 (hardware provided)
Tick accuracy, nanoseconds: 41.67
Tick counter start: 103684134140
Tick counter stop: 103708255194
Elapsed ticks count in 1 seconds: 24121054
Elapsed nanoseconds according to elapsed ticks: 1005043916.67
Comparing the measurement methods using 100 samples:
Elapsed time in nanoseconds, using std::time::Instant
Mean = 60.34
Min = 41.00
Max = 167.00
Standard deviation = 23.92 (39.64 %)
Elapsed time in nanoseconds, using tick_counter
Mean = 42.41
Min = 42.00
Max = 83.00
Standard deviation = 4.08 (9.62 %)
2. Ubuntu 22.04 LTS / Intel® Core™ i7
Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
Linux 6.2.0-31-generic #31~22.04.1-Ubuntu
Output
Basic usage:
Number of elapsed ticks in 1s: 3430864159
Basic usage with helper:
Number of elapsed ticks in 1s: 3430866807
Extended usage:
Environment: linux/unix x86_64
Tick frequency, MHZ: 3430.87 (software estimated in 1s)
Tick accuracy, nanoseconds: 0.29
Tick counter start: 14959335168548
Tick counter stop: 14962766085156
Elapsed ticks count in 1 seconds: 3430916608
Elapsed nanoseconds according to elapsed ticks: 1000013467.72
Comparing results, using 100 samples:
Elapsed time in nanoseconds, using std::time::Instant
Mean = 46.35
Min = 42.00
Max = 262.00
Standard deviation = 21.70 (46.82 %)
Elapsed time in nanoseconds, using tick_counter
Mean = 17.40
Min = 15.00
Max = 18.00
Standard deviation = 0.95 (5.45 %)