Skip to content

Commit a97c243

Browse files
committed
Update to benchmarking
1 parent 3d68843 commit a97c243

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

benches/sort_benchmark.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, BatchSize};
2+
use rand::{distributions::Uniform, Rng};
23
use sorting_algorithm::bubblesort;
34
use sorting_algorithm::mergesort;
45
use sorting_algorithm::quicksort;
56
use sorting_algorithm::insertionsort;
6-
use sorting_algorithm::generate_random_array;
77

8-
fn sort_arrays_benchmark(c: &mut Criterion) {
9-
let mut arr = black_box([-2,-7 ,2 ,10 ,20 ,9]);
8+
fn sorting_benchmarks(c: &mut Criterion) {
9+
let mut group = c.benchmark_group("Sorting Algorithms");
1010

11-
c.bench_function("Bubble Sort", |b| b.iter(|| bubblesort(&mut arr)));
12-
c.bench_function("Merge Sort", |b| b.iter(|| mergesort(&mut arr)));
13-
c.bench_function("Insertion Sort", |b| b.iter(|| insertionsort(&mut arr)));
14-
c.bench_function("Quick Sort", |b| b.iter(|| quicksort(&mut arr)));
15-
}
11+
for i in [1,5,10, 25,50,75, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] { // size of array
12+
let mut rng = rand::thread_rng();
13+
let range = Uniform::new(0, 100); // range of random numbers
1614

17-
fn sort_random_arrays_benchmark(c: &mut Criterion){
18-
let mut random_arr = black_box(generate_random_array(0..100, 10000));
19-
c.bench_function("Bubble Sort - Random array", |b| b.iter(|| bubblesort(&mut random_arr)));
20-
c.bench_function("Merge Sort - Random array", |b| b.iter(|| mergesort(&mut random_arr)));
21-
c.bench_function("Insertion Sort - Random array", |b| b.iter(|| insertionsort(&mut random_arr)));
22-
c.bench_function("Quick Sort - Random array", |b| b.iter(|| quicksort(&mut random_arr)));
15+
// Bubble sort benchmark (Time complexity: O(n^2))
16+
group.bench_function(BenchmarkId::new("Bubble Sort", i), |b| {
17+
b.iter_batched_ref(
18+
|| -> Vec<usize> { (0..i).map(|_| rng.sample(&range)).collect() },
19+
|v| bubblesort( v),
20+
BatchSize::SmallInput,
21+
)
22+
});
23+
// Merge sort benchmark (Time complexity: O(nlogn))
24+
group.bench_function(BenchmarkId::new("Merge Sort", i), |b| {
25+
b.iter_batched_ref(
26+
|| -> Vec<usize> { (0..i).map(|_| rng.sample(&range)).collect() },
27+
|v| mergesort (v),
28+
BatchSize::SmallInput,
29+
)
30+
});
31+
// Insertion sort benchmark (Time complexity: O(n^2))
32+
group.bench_function(BenchmarkId::new("Insertion Sort", i), |b| {
33+
b.iter_batched_ref(
34+
|| -> Vec<usize> { (0..i).map(|_| rng.sample(&range)).collect() },
35+
|v| insertionsort (v),
36+
BatchSize::SmallInput,
37+
)
38+
});
39+
// Quick sort benchmark (Time complexity: O(nlogn))
40+
group.bench_function(BenchmarkId::new("Quick Sort", i), |b| {
41+
b.iter_batched_ref(
42+
|| -> Vec<usize> { (0..i).map(|_| rng.sample(&range)).collect() },
43+
|v| quicksort (v),
44+
BatchSize::SmallInput,
45+
)
46+
});
47+
}
48+
group.finish();
2349
}
2450

25-
criterion_group!(benches, sort_arrays_benchmark, sort_random_arrays_benchmark);
51+
criterion_group!(benches, sorting_benchmarks);
2652
criterion_main!(benches);

0 commit comments

Comments
 (0)