-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsort.rs
60 lines (57 loc) · 2.13 KB
/
sort.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use criterion::{
black_box, criterion_group, criterion_main, AxisScale, BatchSize, Criterion, PlotConfiguration,
};
use ndarray::prelude::*;
use ndarray_slice::Slice1Ext;
use rand::prelude::*;
fn get_from_sorted_mut(c: &mut Criterion) {
let lens = vec![10, 100, 1000, 10000];
let mut group = c.benchmark_group("get_from_sorted_mut");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
for len in &lens {
group.bench_with_input(format!("{}", len), len, |b, &len| {
let mut rng = StdRng::seed_from_u64(42);
let mut data: Vec<_> = (0..len).collect();
data.shuffle(&mut rng);
let indices: Vec<_> = (0..len).step_by(len / 10).collect();
b.iter_batched(
|| Array1::from(data.clone()),
|mut arr| {
for &i in &indices {
black_box(arr.select_nth_unstable(i));
}
},
BatchSize::SmallInput,
)
});
}
group.finish();
}
fn get_many_from_sorted_mut(c: &mut Criterion) {
let lens = vec![10, 100, 1000, 10000];
let mut group = c.benchmark_group("get_many_from_sorted_mut");
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
for len in &lens {
group.bench_with_input(format!("{}", len), len, |b, &len| {
let mut rng = StdRng::seed_from_u64(42);
let mut data: Vec<_> = (0..len).collect();
data.shuffle(&mut rng);
let indices: Array1<_> = (0..len).step_by(len / 10).collect();
b.iter_batched(
|| Array1::from(data.clone()),
|mut arr| {
let mut values = Vec::with_capacity(indices.len());
black_box(arr.select_many_nth_unstable(&indices, &mut values));
},
BatchSize::SmallInput,
)
});
}
group.finish();
}
criterion_group! {
name = benches;
config = Criterion::default();
targets = get_from_sorted_mut, get_many_from_sorted_mut
}
criterion_main!(benches);