Skip to content

Commit 50bc688

Browse files
committed
use quanta to measure time
1 parent b3ce5d0 commit 50bc688

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ yansi = { version = "1.0.1", features = ["detect-env", "detect-tty"] }
3636
rustop = "=1.1.4"
3737
rustc-hash = "2.0.0"
3838
bpu_trasher = { version = "0.2.0", optional = true }
39+
quanta = "0.12"
3940

4041
[target.'cfg(target_os = "linux")'.dependencies]
4142
perf-event = { version = "0.4.8" }
@@ -62,4 +63,3 @@ harness = false
6263
[[bench]]
6364
name = "test_bench"
6465
harness = false
65-

benches/test_bench.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::{
2-
collections::HashMap,
3-
time::{Duration, Instant},
4-
};
1+
use std::{collections::HashMap, time::Duration};
52

63
use binggan::{
74
plugins::{CacheTrasher, PeakMemAllocPlugin, PerfCounterPlugin},
85
BenchRunner, PeakMemAlloc, INSTRUMENTED_SYSTEM,
96
};
7+
use quanta::Instant;
108

119
#[global_allocator]
1210
pub static GLOBAL: &PeakMemAlloc<std::alloc::System> = &INSTRUMENTED_SYSTEM;

src/bench.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
plugins::{alloc::*, *},
66
stats::*,
77
};
8+
use quanta::Instant;
89

910
/// The trait which typically wraps a InputWithBenchmark and allows to hide the generics.
1011
pub trait Bench<'a> {
@@ -183,7 +184,7 @@ impl<'a, I, O: OutputValue> NamedBench<'a, I, O> {
183184
const TARGET_NS_PER_BENCH: u128 = TARGET_MS_PER_BENCH as u128 * 1_000_000;
184185
{
185186
// Preliminary test if function is very slow
186-
let start = std::time::Instant::now();
187+
let start = Instant::now();
187188
#[allow(clippy::unit_arg)]
188189
black_box((self.fun)(input));
189190
let elapsed_ms = start.elapsed().as_millis() as u64;
@@ -192,7 +193,7 @@ impl<'a, I, O: OutputValue> NamedBench<'a, I, O> {
192193
}
193194
}
194195

195-
let start = std::time::Instant::now();
196+
let start = Instant::now();
196197
for _ in 0..64 {
197198
#[allow(clippy::unit_arg)]
198199
black_box((self.fun)(input));
@@ -217,18 +218,24 @@ impl<'a, I, O: OutputValue> NamedBench<'a, I, O> {
217218
bench_id: &self.bench_id,
218219
});
219220
debug_assert!(num_iter > 0);
220-
let start = std::time::Instant::now();
221221

222222
// Defer dropping outputs so destructor cost is not part of the measured time.
223223
let run_result = if O::defer_drop() {
224-
let mut outputs: Vec<O> = Vec::with_capacity(num_iter);
224+
let mut sum_ns = 0u64;
225+
let mut res: Option<O> = None;
226+
// In this mode, we measure each iteration separately to avoid destructor cost.
227+
// There may be some overhead, but it should be outweighed by benchmarks that allocate
225228
for _ in 0..num_iter {
226-
outputs.push(black_box((self.fun)(input)));
229+
res.take();
230+
let start = Instant::now();
231+
let val = black_box((self.fun)(input));
232+
sum_ns += start.elapsed().as_nanos() as u64;
233+
res = Some(val);
227234
}
228-
let duration_ns = start.elapsed().as_nanos() as u64 / num_iter as u64;
229-
let last_output = outputs.pop().expect("num_iter > 0");
230-
RunResult::new(duration_ns, last_output)
235+
let duration_ns = sum_ns / num_iter as u64;
236+
RunResult::new(duration_ns, res.unwrap())
231237
} else {
238+
let start = Instant::now();
232239
let mut res: Option<O> = None;
233240
for _ in 0..num_iter {
234241
res = Some(black_box((self.fun)(input)));

src/bench_runner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ impl BenchRunner {
272272

273273
// In order to make the benchmarks in a group comparable, it is imperative to call them
274274
// the same numer of times
275+
// Note: This doesn't work well for very large groups with very different benchmarks.
275276
let (min_num_iter, max_num_iter) =
276277
minmax(benches.iter_mut().map(|b| b.sample_num_iter())).unwrap();
277278

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ pub struct Config {
2121

2222
impl Default for Config {
2323
fn default() -> Self {
24+
// Check ENV for verbose
25+
let verbose = std::env::var("BINGGAN_VERBOSE").is_ok();
2426
Config {
2527
interleave: true,
2628
filter: None,
27-
verbose: false,
29+
verbose,
2830
num_iter_bench: None,
2931
num_iter_group: None,
3032
}

0 commit comments

Comments
 (0)