2023-10-26 21:33:06 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
|
|
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-05-08 14:26:41 +00:00
|
|
|
const ITERATIONS: u32 = 1_000_000;
|
2023-10-26 21:33:06 +00:00
|
|
|
|
|
|
|
fn bench_move() {
|
|
|
|
let mut value = Arc::new(AtomicU32::new(0));
|
|
|
|
for _ in 0..ITERATIONS {
|
|
|
|
value = do_something_with_move(value);
|
|
|
|
}
|
2024-05-08 14:26:41 +00:00
|
|
|
assert_eq!(value.load(Ordering::Relaxed), ITERATIONS);
|
2023-10-26 21:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn do_something_with_move(value: Arc<AtomicU32>) -> Arc<AtomicU32> {
|
|
|
|
value.fetch_add(1, Ordering::Relaxed);
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_clone() {
|
|
|
|
let value = Arc::new(AtomicU32::new(0));
|
|
|
|
for _ in 0..ITERATIONS {
|
|
|
|
do_something_with_clone(value.clone());
|
|
|
|
}
|
2024-05-08 14:26:41 +00:00
|
|
|
assert_eq!(value.load(Ordering::Relaxed), ITERATIONS);
|
2023-10-26 21:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn do_something_with_clone(value: Arc<AtomicU32>) {
|
|
|
|
value.fetch_add(1, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_move_vs_clone(c: &mut Criterion) {
|
|
|
|
let mut group = c.benchmark_group("move_vs_clone");
|
2024-05-08 14:26:41 +00:00
|
|
|
group.throughput(Throughput::Elements(ITERATIONS as u64));
|
2023-10-26 21:33:06 +00:00
|
|
|
group.sample_size(10);
|
|
|
|
group.measurement_time(Duration::from_secs(10));
|
|
|
|
|
|
|
|
group.bench_function("move", |b| {
|
2023-11-18 13:56:13 +00:00
|
|
|
b.iter(bench_move);
|
2023-10-26 21:33:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group.bench_function("clone", |b| {
|
2023-11-18 13:56:13 +00:00
|
|
|
b.iter(bench_clone);
|
2023-10-26 21:33:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, bench_move_vs_clone);
|
|
|
|
criterion_main!(benches);
|