Skip to content

Instantly share code, notes, and snippets.

@zoni
Created May 14, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoni/e88346705a52972d09fc8e97a2e0b374 to your computer and use it in GitHub Desktop.
Save zoni/e88346705a52972d09fc8e97a2e0b374 to your computer and use it in GitHub Desktop.
Performance benchmark of context() vs with_context()
use anyhow::{Context, Result};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::error::Error;
use std::fmt;
#[derive(Debug)]
struct FooError {}
fn foo() -> Result<(), FooError> {
Err(FooError {})
}
impl Error for FooError {}
impl fmt::Display for FooError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FooError happened")
}
}
fn run() -> Result<()> {
foo().with_context(|| format!("Now calling: {}", "foo()"))?;
Ok(())
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("count_words", |b| b.iter(|| black_box(run())));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
@@ -19,7 +19,7 @@ impl fmt::Display for FooError {
}
fn run() -> Result<()> {
- foo().context(format!("Now calling: {}", "foo()"))?;
+ foo().with_context(|| format!("Now calling: {}", "foo()"))?;
Ok(())
}
➔ cargo bench
Compiling rust-wordcount v0.1.0 (/home/work/Workspace/zoni/rust-wordcount)
Finished bench [optimized] target(s) in 1.74s
Running target/release/deps/wordcount-03e3bc23db692a6a
running 4 tests
test wordcounter::tests::count_multiple_words ... ignored
test wordcounter::tests::count_single_word ... ignored
test wordcounter::tests::empty_input ... ignored
test wordcounter::tests::read_broken_pipe ... ignored
test result: ok. 0 passed; 0 failed; 4 ignored; 0 measured; 0 filtered out
Running target/release/deps/rwc-0b748e838683aeca
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running target/release/deps/context_vs_with_context-aed1599f929f47c2
count_words time: [40.922 ns 41.101 ns 41.311 ns]
Found 12 outliers among 100 measurements (12.00%)
1 (1.00%) low mild
8 (8.00%) high mild
3 (3.00%) high severe
➔ cargo bench
Compiling rust-wordcount v0.1.0 (/home/work/Workspace/zoni/rust-wordcount)
Finished bench [optimized] target(s) in 1.69s
Running target/release/deps/wordcount-03e3bc23db692a6a
running 4 tests
test wordcounter::tests::count_multiple_words ... ignored
test wordcounter::tests::count_single_word ... ignored
test wordcounter::tests::empty_input ... ignored
test wordcounter::tests::read_broken_pipe ... ignored
test result: ok. 0 passed; 0 failed; 4 ignored; 0 measured; 0 filtered out
Running target/release/deps/rwc-0b748e838683aeca
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running target/release/deps/context_vs_with_context-aed1599f929f47c2
count_words time: [40.949 ns 41.086 ns 41.233 ns]
change: [-1.9642% -0.5919% +0.4932%] (p = 0.39 > 0.05)
No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
2 (2.00%) low mild
3 (3.00%) high mild
3 (3.00%) high severe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment