-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from KeplerOps/feature/issue-18-heapsort
Feature/issue 18 heapsort
- Loading branch information
Showing
7 changed files
with
724 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[workspace] | ||
resolver = "2" | ||
members = [ | ||
"crates/shared/blocks-core", | ||
"crates/shared/blocks-utils", | ||
|
@@ -53,6 +54,5 @@ edition = "2021" | |
authors = ["Brad Edwards <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/atomiklabs/blocks" | ||
resolver = "2" | ||
|
||
[workspace.dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
crates/cs/blocks-cs-sort/benches/heapsort_benchmarks.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion, BatchSize}; | ||
use rand::prelude::*; | ||
use blocks_cs_sort::algorithms::heapsort; | ||
|
||
/// Helper to generate different input distributions. | ||
fn generate_data(len: usize, distribution: &str) -> Vec<i32> { | ||
let mut rng = thread_rng(); | ||
let mut data: Vec<i32> = (0..len as i32).collect(); | ||
|
||
match distribution { | ||
"sorted" => { /* already sorted */ }, | ||
"reverse" => data.reverse(), | ||
"random" => data.shuffle(&mut rng), | ||
"nearly_sorted" => { | ||
// Swap every 100th element | ||
for i in 0..(len/100) { | ||
let j = i * 100; | ||
if j + 1 < len { | ||
data.swap(j, j + 1); | ||
} | ||
} | ||
}, | ||
"few_unique" => { | ||
// Only use values 0-9 repeated | ||
for i in 0..len { | ||
data[i] = (i % 10) as i32; | ||
} | ||
}, | ||
_ => {} | ||
}; | ||
data | ||
} | ||
|
||
fn benchmark_heapsort(c: &mut Criterion) { | ||
let sizes = [1_000, 10_000, 100_000, 1_000_000]; | ||
let distributions = ["sorted", "reverse", "random", "nearly_sorted", "few_unique"]; | ||
|
||
let mut group = c.benchmark_group("heapsort"); | ||
group.sample_size(10); // Adjust based on your needs | ||
|
||
for &size in &sizes { | ||
for dist in &distributions { | ||
let bench_name = format!("heapsort_{}_{}", dist, size); | ||
|
||
group.bench_function(&bench_name, |b| { | ||
b.iter_batched( | ||
|| generate_data(size, dist), | ||
|mut data| heapsort::sort(&mut data).expect("Sort should succeed"), | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
} | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
#[cfg(feature = "parallel")] | ||
fn benchmark_parallel_heapsort(c: &mut Criterion) { | ||
let sizes = [100_000, 1_000_000, 10_000_000]; | ||
let distributions = ["sorted", "reverse", "random", "nearly_sorted", "few_unique"]; | ||
|
||
let mut group = c.benchmark_group("parallel_heapsort"); | ||
group.sample_size(10); // Fewer samples for large parallel sorts | ||
|
||
for &size in &sizes { | ||
for dist in &distributions { | ||
let bench_name = format!("parallel_heapsort_{}_{}", dist, size); | ||
|
||
group.bench_function(&bench_name, |b| { | ||
b.iter_batched( | ||
|| generate_data(size, dist), | ||
|mut data| heapsort::sort(&mut data).expect("Sort should succeed"), | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
} | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
fn benchmark_std_sort(c: &mut Criterion) { | ||
let sizes = [1_000, 10_000, 100_000, 1_000_000]; | ||
let distributions = ["sorted", "reverse", "random", "nearly_sorted", "few_unique"]; | ||
|
||
let mut group = c.benchmark_group("std_sort"); | ||
group.sample_size(10); | ||
|
||
for &size in &sizes { | ||
for dist in &distributions { | ||
let bench_name = format!("std_sort_{}_{}", dist, size); | ||
|
||
group.bench_function(&bench_name, |b| { | ||
b.iter_batched( | ||
|| generate_data(size, dist), | ||
|mut data| data.sort(), | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
} | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
#[cfg(feature = "simd")] | ||
fn benchmark_simd_sort(c: &mut Criterion) { | ||
let sizes = [1_000, 10_000, 100_000, 1_000_000]; | ||
let distributions = ["sorted", "reverse", "random", "nearly_sorted", "few_unique"]; | ||
|
||
let mut group = c.benchmark_group("simd_sort"); | ||
group.sample_size(10); | ||
|
||
for &size in &sizes { | ||
for dist in &distributions { | ||
let bench_name = format!("simd_sort_{}_{}", dist, size); | ||
|
||
group.bench_function(&bench_name, |b| { | ||
b.iter_batched( | ||
|| generate_data(size, dist), | ||
|mut data| heapsort::sort_i32(&mut data).expect("Sort should succeed"), | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
} | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = Criterion::default(); | ||
targets = benchmark_heapsort, benchmark_std_sort | ||
} | ||
|
||
#[cfg(feature = "parallel")] | ||
criterion_group! { | ||
name = parallel_benches; | ||
config = Criterion::default(); | ||
targets = benchmark_parallel_heapsort | ||
} | ||
|
||
#[cfg(feature = "simd")] | ||
criterion_group! { | ||
name = simd_benches; | ||
config = Criterion::default(); | ||
targets = benchmark_simd_sort | ||
} | ||
|
||
// Base benchmarks always run | ||
#[cfg(not(any(feature = "parallel", feature = "simd")))] | ||
criterion_main!(benches); | ||
|
||
// With parallel feature only | ||
#[cfg(all(feature = "parallel", not(feature = "simd")))] | ||
criterion_main!(benches, parallel_benches); | ||
|
||
// With SIMD feature only | ||
#[cfg(all(feature = "simd", not(feature = "parallel")))] | ||
criterion_main!(benches, simd_benches); | ||
|
||
// With both parallel and SIMD features | ||
#[cfg(all(feature = "parallel", feature = "simd"))] | ||
criterion_main!(benches, parallel_benches, simd_benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// Sorting algorithms | ||
pub mod quicksort; | ||
pub mod heapsort; | ||
|
||
// Re-export commonly used functions for convenience | ||
pub use self::quicksort::sort as quicksort; | ||
pub use self::quicksort::sort as quicksort; | ||
pub use self::heapsort::sort as heapsort; |
Oops, something went wrong.