Skip to content

Instantly share code, notes, and snippets.

@tobz
Created December 30, 2023 15:33
Show Gist options
  • Save tobz/79f207c7af13f792af42f4845af853a4 to your computer and use it in GitHub Desktop.
Save tobz/79f207c7af13f792af42f4845af853a4 to your computer and use it in GitHub Desktop.
[package]
name = "quanta-stdlib-deviation"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
quanta = "0.12.1"
hdrhistogram = "7.5.4"
use std::{thread, time::Duration, io::Write as _};
use hdrhistogram::Histogram;
const SLEEP_DUR: Duration = Duration::from_secs(1);
fn main() {
const N: usize = 20;
measure_elapsed("std", N, elapsed_std);
measure_elapsed("quanta", N, elapsed_quanta);
}
fn measure_elapsed<F>(ty: &str, repeat: usize, f: F)
where
F: Fn() -> Duration,
{
let mut histo = Histogram::<u64>::new(5).unwrap();
println!("running test for {ty}...");
for _ in 1..=repeat {
let elapsed = f();
histo.record(elapsed.as_nanos() as u64).unwrap();
print!(".");
std::io::stdout().flush().unwrap();
}
println!(" done!\n");
let min = histo.min();
let p50 = histo.value_at_quantile(0.5);
let p99 = histo.value_at_quantile(0.99);
let max = histo.max();
println!("{ty} statistics:");
println!(" min: {min}ns\n p50: {p50}ns\n p99: {p99}ns\n max: {max}ns");
let range = max - min;
let sleep_dur_ns = SLEEP_DUR.as_nanos() as u64;
let lower_pct = (min as f64 / sleep_dur_ns as f64) * 100.0;
let upper_pct = (max as f64 / sleep_dur_ns as f64) * 100.0;
println!(" range: {range}ns ({lower_pct:.5}% to {upper_pct:.5}% of target)");
println!("");
}
fn elapsed_std() -> Duration {
let start = std::time::Instant::now();
thread::sleep(SLEEP_DUR);
start.elapsed()
}
fn elapsed_quanta() -> Duration {
let start = quanta::Instant::now();
thread::sleep(SLEEP_DUR);
start.elapsed()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment