Skip to content

Commit cb91ae7

Browse files
committed
Added benchmark, edited Cargo and documentation
1 parent cdc4351 commit cb91ae7

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ version = "1.1.0"
44
authors = ["flakusha <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/flakusha/rust_sorting"
7+
description = "Collection of sorting algorithms implemented in Rust"
78

89
keywords = ["sort", "sorting", "library"]
910
categories = ["algorithms"]
1011

1112
license = "MIT"
1213
readme = "README.md"
13-
1414
autobenches = false
1515

16-
include = [
17-
"/src/**",
18-
"/bench/**",
19-
]
2016
# See more keys and their definitions at
2117
# https://doc.rust-lang.org/cargo/reference/manifest.html
2218

2319
[dependencies]
2420

2521
[dev-dependencies]
26-
rand = "^0.7.3"
22+
rand = "^0.7.3"
23+
criterion = "^0.3"
24+
25+
[[bench]]
26+
name = "sort_benchmark"
27+
harness = false

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# sorting_rs
2-
## This library contains following sorting algorithms implemented in Rust:
2+
Sorting algorithms implemented in Rust
3+
## Usage
4+
1. Add this dependency with it's version into your Cargo.toml:
5+
```toml
6+
...
7+
[dependencies]
8+
sorting_rs = "1.1.0"
9+
...
10+
```
11+
2. Use available sorting algorithms in your Rust code:
12+
```rust
13+
use sorting_rs::*;
14+
...
15+
```
16+
3. For more information about origin of algorithms and implementation details,
17+
please read modules documentation
18+
19+
## This library contains following sorting algorithms:
20+
New algorithms implementations are planned, smooth sort is not ready at the
21+
moment, for example
322

423
| Sorting algorithm | Features and downsides | Worst-case performance O(): comparisons; swaps | Best-case performance O(): comparisons; swaps | Space complexity O() |
524
| ------ | -------------------------------- | ------------------------------------ | -------- | ---------- |

benches/sort_benchmark.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// #[macro_use]
2+
use criterion::{
3+
criterion_group, criterion_main,
4+
Criterion, Bencher, ParameterizedBenchmark
5+
};
6+
use rand::prelude::*;
7+
8+
fn get_random_vec(n: usize) -> Vec<usize> {
9+
let mut rng: StdRng = StdRng::seed_from_u64(42);
10+
let mut vec: Vec<usize> = (0..n).collect();
11+
vec.shuffle(&mut rng);
12+
vec
13+
}
14+
15+
macro_rules! create_bench_function {
16+
($x:ident) => {
17+
|b: &mut Bencher, n: &usize| {
18+
let s = get_random_vec(*n);
19+
b.iter(|| sorting_rs::$x(&mut s.clone()));
20+
}
21+
};
22+
}
23+
24+
macro_rules! create_bench {
25+
($p: expr, $f: ident, $($s: ident), *) => {
26+
ParameterizedBenchmark::new(stringify!($f),
27+
create_bench_function!($f), $p)
28+
$(.with_function(stringify!($s), create_bench_function!($s)))*
29+
}
30+
}
31+
32+
fn bench(c: &mut Criterion) {
33+
let sizes: Vec<usize> = vec![10, 100, 1000, 10_000, 100_000, /*1_000_000,
34+
10_000_000*/];
35+
36+
let benchmark = create_bench! {
37+
sizes,
38+
bubble_sort,
39+
cocktail_sort,
40+
comb_sort,
41+
gnome_sort,
42+
heap_sort,
43+
heap_bottom_up_sort,
44+
insertion_sort,
45+
merge_sort,
46+
oddeven_sort,
47+
quick_sort,
48+
selection_sort,
49+
shell_sort,
50+
slow_sort,
51+
// smooth_sort,
52+
stooge_sort
53+
};
54+
55+
c.bench("sort_bench", benchmark);
56+
}
57+
58+
criterion_group!(benches, bench);
59+
criterion_main!(benches);

0 commit comments

Comments
 (0)