added benchmark for `threadpool`

using `criterion`.
This commit is contained in:
Sven Vogel 2023-05-31 09:00:49 +02:00
parent 990a54a032
commit c732864a74
3 changed files with 82 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target /target
/Cargo.lock /Cargo.lock
.DS_Store .DS_Store
/.vscode

View File

@ -10,3 +10,10 @@ authors = ["Sven Vogel", "Felix L. Müller", "Elias Alexander", "Elias Schmidt"]
png = "0.17.8" png = "0.17.8"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
[dev-dependencies]
criterion = "0.5.1"
[[bench]]
name = "multithreading"
harness = false

74
benches/multithreading.rs Normal file
View File

@ -0,0 +1,74 @@
use std::sync::Arc;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use imsearch::multithreading::ThreadPool;
fn dot(a: &[f64], b: &[f64]) -> f64 {
let mut sum = 0.0;
for i in 0..a.len() {
sum += a[i] * b[i];
}
sum
}
fn bench_single_threaded(a: &Vec<f64>, b: &Vec<f64>) {
black_box(dot(a, b));
}
fn bench_threadpool(a: Arc<Vec<f64>>, b: Arc<Vec<f64>>) {
let mut pool = ThreadPool::new();
const CHUNKS: usize = 100;
let steps = a.len() / CHUNKS;
for i in 0..CHUNKS {
let chunk = i * steps;
let aa = a.clone();
let bb = b.clone();
pool.enqueue(move || {
let a = &aa[chunk..(chunk + steps)];
let b = &bb[chunk..(chunk + steps)];
dot(a, b)
});
}
black_box(
pool.join_all()
.into_iter()
.map(|r| r.unwrap())
.reduce(|a, b| a + b),
);
}
#[inline]
fn hash(x: f64) -> f64 {
((x * 234.8743 + 3.8274).sin() * 87624.58376).fract()
}
fn create_vec(size: usize) -> Arc<Vec<f64>> {
let mut vec = Vec::with_capacity(size);
for i in 0..size {
vec.push(hash(i as f64));
}
Arc::new(vec)
}
pub fn benchmark_threadpool(c: &mut Criterion) {
let vec_a = create_vec(1_000_000);
let vec_b = create_vec(1_000_000);
c.bench_function("single threaded", |b| {
b.iter(|| bench_single_threaded(&vec_a, &vec_b))
});
c.bench_function("multi threaded", |b| {
b.iter(|| bench_threadpool(vec_a.clone(), vec_b.clone()))
});
}
criterion_group!(benches, benchmark_threadpool);
criterion_main!(benches);