Skip to content

Instantly share code, notes, and snippets.

@TennyZhuang
Last active August 9, 2022 08:13
Show Gist options
  • Save TennyZhuang/3526b8cd612366a61f1baef5120b02a3 to your computer and use it in GitHub Desktop.
Save TennyZhuang/3526b8cd612366a61f1baef5120b02a3 to your computer and use it in GitHub Desktop.
[package]
name = "my_benchmark"
version = "0.1.0"
edition = "2021"
[dev-dependencies]
criterion = { version = "0.3", features = ["async_futures"]}
[[bench]]
name = "my_benchmark"
harness = false
extern crate criterion;
use criterion::async_executor::FuturesExecutor;
use criterion::*;
#[inline(always)]
fn add(num: &mut u64, rhs: u64) {
*num += rhs
}
#[inline(always)]
fn add_one(num: &mut u64) {
add(num, 1)
}
#[inline(always)]
async fn add_one_async(num: &mut u64) {
add(num, 1)
}
fn bench_add_one(c: &mut Criterion) {
c.bench_function("bench_normal_add_one", |b| {
b.to_async(FuturesExecutor).iter(|| {
async move {
let ADD_ONE_COUNT: u64 = black_box(100_000_000);
let mut num = 0;
for _ in 0..ADD_ONE_COUNT {
add_one(&mut num);
}
assert_eq!(num, ADD_ONE_COUNT);
}
});
});
c.bench_function("bench_async_add_one", |b| {
b.to_async(FuturesExecutor).iter(|| {
async move {
let ADD_ONE_COUNT: u64 = black_box(100_000_000);
let mut num = 0;
for _ in 0..ADD_ONE_COUNT {
add_one_async(&mut num).await;
}
assert_eq!(num, ADD_ONE_COUNT);
}
});
});
}
criterion_group!(
benches,
bench_add_one
);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment