From cb91ae715486d2ab44b8f507912e662e8d6056a0 Mon Sep 17 00:00:00 2001 From: Constantine Date: Sun, 12 Jul 2020 15:36:10 +0300 Subject: [PATCH] Added benchmark, edited Cargo and documentation --- Cargo.toml | 13 +++++---- README.md | 21 +++++++++++++- benches/sort_benchmark.rs | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 benches/sort_benchmark.rs diff --git a/Cargo.toml b/Cargo.toml index 542d7b1..6a32438 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,23 +4,24 @@ version = "1.1.0" authors = ["flakusha "] edition = "2018" repository = "https://github.com/flakusha/rust_sorting" +description = "Collection of sorting algorithms implemented in Rust" keywords = ["sort", "sorting", "library"] categories = ["algorithms"] license = "MIT" readme = "README.md" - autobenches = false -include = [ - "/src/**", - "/bench/**", -] # See more keys and their definitions at # https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] [dev-dependencies] -rand = "^0.7.3" \ No newline at end of file +rand = "^0.7.3" +criterion = "^0.3" + +[[bench]] +name = "sort_benchmark" +harness = false \ No newline at end of file diff --git a/README.md b/README.md index 93b126c..b86a0ec 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,24 @@ # sorting_rs -## This library contains following sorting algorithms implemented in Rust: +Sorting algorithms implemented in Rust +## Usage +1. Add this dependency with it's version into your Cargo.toml: +```toml +... +[dependencies] +sorting_rs = "1.1.0" +... +``` +2. Use available sorting algorithms in your Rust code: +```rust +use sorting_rs::*; +... +``` +3. For more information about origin of algorithms and implementation details, +please read modules documentation + +## This library contains following sorting algorithms: +New algorithms implementations are planned, smooth sort is not ready at the +moment, for example | Sorting algorithm | Features and downsides | Worst-case performance O(): comparisons; swaps | Best-case performance O(): comparisons; swaps | Space complexity O() | | ------ | -------------------------------- | ------------------------------------ | -------- | ---------- | diff --git a/benches/sort_benchmark.rs b/benches/sort_benchmark.rs new file mode 100644 index 0000000..7013fa8 --- /dev/null +++ b/benches/sort_benchmark.rs @@ -0,0 +1,59 @@ +// #[macro_use] +use criterion::{ + criterion_group, criterion_main, + Criterion, Bencher, ParameterizedBenchmark +}; +use rand::prelude::*; + +fn get_random_vec(n: usize) -> Vec { + let mut rng: StdRng = StdRng::seed_from_u64(42); + let mut vec: Vec = (0..n).collect(); + vec.shuffle(&mut rng); + vec +} + +macro_rules! create_bench_function { + ($x:ident) => { + |b: &mut Bencher, n: &usize| { + let s = get_random_vec(*n); + b.iter(|| sorting_rs::$x(&mut s.clone())); + } + }; +} + +macro_rules! create_bench { + ($p: expr, $f: ident, $($s: ident), *) => { + ParameterizedBenchmark::new(stringify!($f), + create_bench_function!($f), $p) + $(.with_function(stringify!($s), create_bench_function!($s)))* + } +} + +fn bench(c: &mut Criterion) { + let sizes: Vec = vec![10, 100, 1000, 10_000, 100_000, /*1_000_000, + 10_000_000*/]; + + let benchmark = create_bench! { + sizes, + bubble_sort, + cocktail_sort, + comb_sort, + gnome_sort, + heap_sort, + heap_bottom_up_sort, + insertion_sort, + merge_sort, + oddeven_sort, + quick_sort, + selection_sort, + shell_sort, + slow_sort, + // smooth_sort, + stooge_sort + }; + + c.bench("sort_bench", benchmark); +} + +criterion_group!(benches, bench); +criterion_main!(benches); \ No newline at end of file