#benchmark #rdtsc #cntvct-el0 #rdtscp #cntfrq-el0

bin+lib tick_counter

基于硬件的tick计数器,用于高精度基准测试

13 个版本

0.4.5 2023年9月4日
0.4.4 2023年9月3日
0.4.1 2023年8月4日
0.3.4 2023年8月1日
0.2.1 2023年7月7日

#184硬件支持

Download history 179/week @ 2024-04-20 230/week @ 2024-04-27 399/week @ 2024-05-04 59/week @ 2024-05-11 54/week @ 2024-05-18 180/week @ 2024-05-25 253/week @ 2024-06-01 264/week @ 2024-06-08 244/week @ 2024-06-15 251/week @ 2024-06-22 314/week @ 2024-06-29 350/week @ 2024-07-06 375/week @ 2024-07-13 127/week @ 2024-07-20 173/week @ 2024-07-27 65/week @ 2024-08-03

793 每月下载量
用于 2 crates

MIT 许可证

22KB
349 代码行

基于硬件的tick计数器,用于高精度基准测试

crates.io docs build & test license

  • x86_64: 执行 RDTSC CPU 指令读取时间戳计数器。
  • AArch64: 读取 CNTVCT_EL0 计时器寄存器的值。

简要描述: https://sheroz.com/pages/blog/rust-hardware-based-tick-counters-for-high-precision-benchmarks.html

测试平台

x86_64 (Intel® Core™ i7)
AArch64 (Apple M1 Pro)

用法

请参阅 src/bin/sample.rs 以获取使用示例

基本用法

let start = tick_counter::start();
// ... lines of code to benchmark
let elapsed_ticks = tick_counter::stop() - start;
println!("Number of elapsed ticks: {}", elapsed_ticks);

使用辅助工具的基本用法

use tick_counter::TickCounter;
 
let tick_counter = TickCounter::current();
// ... lines of code to benchmark
let elapsed_ticks = tick_counter.elapsed();
println!("Number of elapsed ticks: {}", elapsed_ticks);

扩展用法

println!("Environment: {}/{} {}", consts::OS, consts::FAMILY, consts::ARCH);

let (counter_frequency, accuracy) = tick_counter::frequency();
let frequency_base = match accuracy {
    tick_counter::TickCounterFrequencyBase::Hardware => "hardware provided".to_string(),
    tick_counter::TickCounterFrequencyBase::Measured(duration) => format!("software estimated in {:?}", duration)
};
println!("Tick frequency, MHZ: {:.2} ({})", counter_frequency as f64 / 1e6_f64, frequency_base);

let counter_accuracy = tick_counter::precision_nanoseconds(counter_frequency);
println!("Tick accuracy, nanoseconds: {:.2}", counter_accuracy);

let counter_start = tick_counter::start();
thread::sleep(time::Duration::from_secs(1));
let counter_stop = tick_counter::stop();

println!("Tick counter start: {}", counter_start);
println!("Tick counter stop: {}", counter_stop);

let elapsed_ticks = counter_stop - counter_start;
println!("Elapsed ticks count in 1 seconds: {}", elapsed_ticks);

let elapsed_nanoseconds = (elapsed_ticks as f64) * counter_accuracy;
println!("Elapsed nanoseconds according to elapsed ticks: {:.2}", elapsed_nanoseconds);

输出

1. 2021款Macbook Pro 16 / Apple Silicon

Apple M1 Pro
MacOS Ventura 13.5.1, Darwin Kernel Version 22.6.0

输出

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

输出

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 %)

无运行时依赖