Last active
August 9, 2022 08:13
-
-
Save TennyZhuang/3526b8cd612366a61f1baef5120b02a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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