Skip to content

Commit

Permalink
Improve: Report throughput in Rust benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Nov 12, 2024
1 parent 6f69eee commit cf507db
Showing 1 changed file with 59 additions and 49 deletions.
108 changes: 59 additions & 49 deletions scripts/bench_similarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rand::Rng;
use std::ops::{AddAssign, Mul};

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use num_traits::{AsPrimitive, Num, NumCast};
use simsimd::SpatialSimilarity as SimSIMD;

Expand Down Expand Up @@ -223,30 +223,36 @@ pub fn l2sq_benchmark(c: &mut Criterion) {
generate_random_vector_u8(DIMENSIONS),
);

let mut group = c.benchmark_group("Squared Euclidean Distance");

for i in 0..=5 {
group.bench_with_input(BenchmarkId::new("SimSIMD f32", i), &i, |b, _| {
b.iter(|| SimSIMD::sqeuclidean(&inputs_f32.0, &inputs_f32.1))
});
group.bench_with_input(BenchmarkId::new("SimSIMD i8", i), &i, |b, _| {
b.iter(|| SimSIMD::sqeuclidean(&inputs_i8.0, &inputs_i8.1))
});
group.bench_with_input(BenchmarkId::new("SimSIMD u8", i), &i, |b, _| {
b.iter(|| SimSIMD::sqeuclidean(&inputs_u8.0, &inputs_u8.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled i8", i), &i, |b, _| {
b.iter(|| baseline_l2sq_unrolled::<i8, i32>(&inputs_i8.0, &inputs_i8.1))
});

group.bench_with_input(BenchmarkId::new("Rust Unrolled u8", i), &i, |b, _| {
b.iter(|| baseline_l2sq_unrolled::<u8, u32>(&inputs_u8.0, &inputs_u8.1))
});

group.bench_with_input(BenchmarkId::new("Rust Unrolled f32", i), &i, |b, _| {
b.iter(|| baseline_l2sq_unrolled::<f32, f32>(&inputs_f32.0, &inputs_f32.1))
});
}
let mut group: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> =
c.benchmark_group("Squared Euclidean Distance");
group.throughput(Throughput::Elements(DIMENSIONS as u64));

// f32
group.throughput(Throughput::Bytes((DIMENSIONS * size_of::<f32>()) as u64));
group.bench_with_input(BenchmarkId::new("SimSIMD f32", 0), &0, |b, _| {
b.iter(|| SimSIMD::sqeuclidean(&inputs_f32.0, &inputs_f32.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled f32", 0), &0, |b, _| {
b.iter(|| baseline_l2sq_unrolled::<f32, f32>(&inputs_f32.0, &inputs_f32.1))
});

// i8
group.throughput(Throughput::Bytes((DIMENSIONS * size_of::<i8>()) as u64));
group.bench_with_input(BenchmarkId::new("SimSIMD i8", 0), &0, |b, _| {
b.iter(|| SimSIMD::sqeuclidean(&inputs_i8.0, &inputs_i8.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled i8", 0), &0, |b, _| {
b.iter(|| baseline_l2sq_unrolled::<i8, i32>(&inputs_i8.0, &inputs_i8.1))
});

// u8
group.throughput(Throughput::Bytes((DIMENSIONS * size_of::<u8>()) as u64));
group.bench_with_input(BenchmarkId::new("SimSIMD u8", 0), &0, |b, _| {
b.iter(|| SimSIMD::sqeuclidean(&inputs_u8.0, &inputs_u8.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled u8", 0), &0, |b, _| {
b.iter(|| baseline_l2sq_unrolled::<u8, u32>(&inputs_u8.0, &inputs_u8.1))
});
}

pub fn cos_benchmark(c: &mut Criterion) {
Expand All @@ -264,30 +270,34 @@ pub fn cos_benchmark(c: &mut Criterion) {
);

let mut group = c.benchmark_group("Cosine Similarity");

for i in 0..=5 {
group.bench_with_input(BenchmarkId::new("SimSIMD f32", i), &i, |b, _| {
b.iter(|| SimSIMD::cosine(&inputs_f32.0, &inputs_f32.1))
});
group.bench_with_input(BenchmarkId::new("SimSIMD i8", i), &i, |b, _| {
b.iter(|| SimSIMD::cosine(&inputs_i8.0, &inputs_i8.1))
});
group.bench_with_input(BenchmarkId::new("SimSIMD u8", i), &i, |b, _| {
b.iter(|| SimSIMD::cosine(&inputs_u8.0, &inputs_u8.1))
});

group.bench_with_input(BenchmarkId::new("Rust Unrolled i8", i), &i, |b, _| {
b.iter(|| baseline_cos_unrolled::<i8, i32>(&inputs_i8.0, &inputs_i8.1))
});

group.bench_with_input(BenchmarkId::new("Rust Unrolled u8", i), &i, |b, _| {
b.iter(|| baseline_cos_unrolled::<u8, u32>(&inputs_u8.0, &inputs_u8.1))
});

group.bench_with_input(BenchmarkId::new("Rust Unrolled f32", i), &i, |b, _| {
b.iter(|| baseline_cos_unrolled::<f32, f32>(&inputs_f32.0, &inputs_f32.1))
});
}
group.throughput(Throughput::Elements(DIMENSIONS as u64));

// f32
group.throughput(Throughput::Bytes((DIMENSIONS * size_of::<f32>()) as u64));
group.bench_with_input(BenchmarkId::new("SimSIMD f32", 0), &0, |b, _| {
b.iter(|| SimSIMD::cosine(&inputs_f32.0, &inputs_f32.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled u8", 0), &0, |b, _| {
b.iter(|| baseline_cos_unrolled::<u8, u32>(&inputs_u8.0, &inputs_u8.1))
});

// i8
group.throughput(Throughput::Bytes((DIMENSIONS * size_of::<i8>()) as u64));
group.bench_with_input(BenchmarkId::new("SimSIMD i8", 0), &0, |b, _| {
b.iter(|| SimSIMD::cosine(&inputs_i8.0, &inputs_i8.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled i8", 0), &0, |b, _| {
b.iter(|| baseline_cos_unrolled::<i8, i32>(&inputs_i8.0, &inputs_i8.1))
});

// u8
group.throughput(Throughput::Bytes((DIMENSIONS * size_of::<u8>()) as u64));
group.bench_with_input(BenchmarkId::new("SimSIMD u8", 0), &0, |b, _| {
b.iter(|| SimSIMD::cosine(&inputs_u8.0, &inputs_u8.1))
});
group.bench_with_input(BenchmarkId::new("Rust Unrolled f32", 0), &0, |b, _| {
b.iter(|| baseline_cos_unrolled::<f32, f32>(&inputs_f32.0, &inputs_f32.1))
});
}

// Criterion group definitions
Expand Down

0 comments on commit cf507db

Please sign in to comment.