x86_64: executes RDTSC CPU instruction to read the time-stamp counter.
AArch64: reads value of the CNTVCT_EL0 counter-timer register.
text
x86_64 (Intel® Core™ i7)
AArch64 (Apple M1 Pro)
For usage samples please look at src/bin/sample.rs
rust
let start = tick_counter::start();
// ... lines of code to benchmark
let elapsed_ticks = tick_counter::stop() - start;
println!("Number of elapsed ticks: {}", elapsed_ticks);
```rust use tick_counter::TickCounter;
let tickcounter = TickCounter::current(); // ... lines of code to benchmark let elapsedticks = tickcounter.elapsed(); println!("Number of elapsed ticks: {}", elapsedticks); ```
```rust println!("Environment: {}/{} {}", consts::OS, consts::FAMILY, consts::ARCH);
let (counterfrequency, accuracy) = tickcounter::frequency(); let frequencybase = match accuracy { tickcounter::TickCounterFrequencyBase::Hardware => "hardware provided".tostring(), tickcounter::TickCounterFrequencyBase::Measured(duration) => format!("software estimated in {:?}", duration) }; println!("Tick frequency, MHZ: {:.2} ({})", counterfrequency as f64 / 1e6f64, frequency_base);
let counteraccuracy = tickcounter::precisionnanoseconds(counterfrequency); println!("Tick accuracy, nanoseconds: {:.2}", counter_accuracy);
let counterstart = tickcounter::start(); thread::sleep(time::Duration::fromsecs(1)); let counterstop = tick_counter::stop();
println!("Tick counter start: {}", counterstart); println!("Tick counter stop: {}", counterstop);
let elapsedticks = counterstop - counterstart; println!("Elapsed ticks count in 1 seconds: {}", elapsedticks);
let elapsednanoseconds = (elapsedticks as f64) * counteraccuracy; println!("Elapsed nanoseconds according to elapsed ticks: {:.2}", elapsednanoseconds); ```
text
Apple M1 Pro
MacOS Ventura 13.5.1, Darwin Kernel Version 22.6.0
Output
```text 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 %) ```
text
Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
Linux 6.2.0-31-generic #31~22.04.1-Ubuntu
Output
```text Basic usage: Number of elapsed ticks in 1s: 3430067918
Basic usage with helper: Number of elapsed ticks in 1s: 3430099298
Extended usage: Environment: linux/unix x86_64 Tick frequency, MHZ: 3430.06 (software estimated in 1s) Tick accuracy, nanoseconds: 0.29 Tick counter start: 3333113402540 Tick counter stop: 3336543463266 Elapsed ticks count in 1 seconds: 3430060726 Elapsed nanoseconds according to elapsed ticks: 1000000004.66
Comparing the measurement methods using 100 samples: Elapsed time in nanoseconds, using std::time::Instant Mean = 47.26 Min = 42.00 Max = 434.00 Standard deviation = 38.89 (82.29 %) Elapsed time in nanoseconds, using tick_counter Mean = 16.88 Min = 15.00 Max = 18.00 Standard deviation = 1.34 (7.92 %) ```