Skip to content

Commit

Permalink
perf: add benchmark for get_all_subset_sums
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed Oct 21, 2024
1 parent 4aa1800 commit d117859
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions crates/subset-sum/benches/get_all_subset_sum.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion};
use subset_sum::{get_all_subset_sums, SubsetSumAllResult};

fn benchmark_all_subset_sums(c: &mut Criterion) {
// Define a sample list and sum for benchmarking
let list = vec![3, 34, 4, 12, 5, 2];
let sum = 9;
let timeout_in_ms = None;
let mut group = c.benchmark_group("all_subset_sums");

// Benchmark the get_all_subset_sums function
c.bench_function("get_all_subset_sums", |b| {
group.bench_function("original case", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> =
get_all_subset_sums(list.clone(), sum, timeout_in_ms);
criterion::black_box(result)
get_all_subset_sums(vec![3, 34, 4, 12, 5, 2], 9, None);
black_box(result)
});
});

group.bench_function("small list, multiple solutions", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> = get_all_subset_sums(vec![1, 2, 3, 4, 5], 5, None);
black_box(result)
});
});

group.bench_function("medium list, few solutions", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> = get_all_subset_sums((1..20).collect(), 38, None);
black_box(result)
});
});

group.bench_function("list with duplicates", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> =
get_all_subset_sums(vec![1, 2, 2, 3, 3, 4, 5], 6, None);
black_box(result)
});
});

group.bench_function("no solution case", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> =
get_all_subset_sums(vec![10, 20, 30, 40], 5, None);
black_box(result)
});
});

group.bench_function("large list with timeout", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> =
get_all_subset_sums((1..30).collect(), 100, Some(100));
black_box(result)
});
});

group.finish();
}

criterion_group!(benches, benchmark_all_subset_sums);
Expand Down

0 comments on commit d117859

Please sign in to comment.