Skip to content

Commit

Permalink
Added benchmark, edited Cargo and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
flakusha committed Jul 12, 2020
1 parent cdc4351 commit cb91ae7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ version = "1.1.0"
authors = ["flakusha <[email protected]>"]
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"
rand = "^0.7.3"
criterion = "^0.3"

[[bench]]
name = "sort_benchmark"
harness = false
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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() |
| ------ | -------------------------------- | ------------------------------------ | -------- | ---------- |
Expand Down
59 changes: 59 additions & 0 deletions benches/sort_benchmark.rs
Original file line number Diff line number Diff line change
@@ -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<usize> {
let mut rng: StdRng = StdRng::seed_from_u64(42);
let mut vec: Vec<usize> = (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<usize> = 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);

0 comments on commit cb91ae7

Please sign in to comment.