Skip to content

Commit

Permalink
Merge pull request #12 from julianbuettner/main
Browse files Browse the repository at this point in the history
Missing spaces make me unhappy
  • Loading branch information
nicku12345 committed Oct 10, 2023
2 parents 2dcbfb7 + 41f2143 commit a022483
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 243 deletions.
111 changes: 62 additions & 49 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use rand_chacha::ChaCha8Rng;
use sortedlist_rs::SortedList;
use criterion::{
criterion_group,
criterion_main,
Criterion,
BenchmarkId,
PlotConfiguration,
AxisScale
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use sortedlist_rs::SortedList;

const DEFAULT_TEST_SIZES: [usize; 5] = [100, 1_000, 10_000, 100_000, 1_000_000];

Expand All @@ -18,11 +13,7 @@ fn get_random_number_generator() -> impl Rng {

fn get_random_array(test_size: usize) -> Vec<i32> {
let mut rng = get_random_number_generator();
(0..test_size)
.map(|_| {
rng.gen::<i32>()
})
.collect()
(0..test_size).map(|_| rng.gen::<i32>()).collect()
}

fn insert_random_element_benchmark(c: &mut Criterion) {
Expand All @@ -32,7 +23,7 @@ fn insert_random_element_benchmark(c: &mut Criterion) {
for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| (get_random_array(*size), SortedList::new()),
Expand All @@ -41,8 +32,10 @@ fn insert_random_element_benchmark(c: &mut Criterion) {
sorted_list.insert(*x);
}
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}
Expand All @@ -54,7 +47,7 @@ fn remove_first_element_benchmark(c: &mut Criterion) {
for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| SortedList::from(get_random_array(*size)),
Expand All @@ -63,20 +56,22 @@ fn remove_first_element_benchmark(c: &mut Criterion) {
sorted_list.remove(0);
}
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}

fn remove_last_element_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Remove last element");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| SortedList::from(get_random_array(*size)),
Expand All @@ -85,42 +80,46 @@ fn remove_last_element_benchmark(c: &mut Criterion) {
sorted_list.remove(i);
}
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}

fn remove_middle_element_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Remove middle element");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| SortedList::from(get_random_array(*size)),
|sorted_list| {
for i in (0..*size).rev() {
sorted_list.remove(i/2);
sorted_list.remove(i / 2);
}
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}

fn remove_random_element_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Remove random element");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| {
Expand All @@ -129,59 +128,73 @@ fn remove_random_element_benchmark(c: &mut Criterion) {
SortedList::from(get_random_array(*size)),
(0..*size)
.rev()
.map(|i| {
rng.gen_range(0..i+1)
})
.collect::<Vec<usize>>()
.map(|i| rng.gen_range(0..i + 1))
.collect::<Vec<usize>>(),
)
},
|(sorted_list, iter)| {
for i in iter {
sorted_list.remove(*i);
}
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}

fn get_random_element_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Get random element");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| (SortedList::from(get_random_array(*size)), get_random_number_generator()),
|| {
(
SortedList::from(get_random_array(*size)),
get_random_number_generator(),
)
},
|(sorted_list, rng)| {
sorted_list.get(rng.gen_range(0..*size+1));
sorted_list.get(rng.gen_range(0..*size + 1));
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}

fn binary_search_random_element_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Binary search random element");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

for test_size in DEFAULT_TEST_SIZES.iter() {
group.bench_with_input(
BenchmarkId::new("SortedList", test_size),
test_size,
test_size,
|b, size| {
b.iter_batched_ref(
|| (SortedList::from(get_random_array(*size)), get_random_number_generator()),
|| {
(
SortedList::from(get_random_array(*size)),
get_random_number_generator(),
)
},
|(sorted_list, rng)| {
let _result = sorted_list.binary_search(&rng.gen::<i32>());
let _result = sorted_list.binary_search(&rng.gen::<i32>());
},
criterion::BatchSize::SmallInput);
});
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}
Expand All @@ -196,4 +209,4 @@ criterion_group!(
get_random_element_benchmark,
binary_search_random_element_benchmark,
);
criterion_main!(benches);
criterion_main!(benches);
Loading

0 comments on commit a022483

Please sign in to comment.