|
1 | 1 | #![feature(test)]
|
| 2 | +#![allow(non_snake_case)] |
2 | 3 |
|
3 | 4 | extern crate test;
|
4 | 5 | extern crate rand;
|
@@ -27,28 +28,31 @@ fn seq_slice_choose_1_of_1000(b: &mut Bencher) {
|
27 | 28 | })
|
28 | 29 | }
|
29 | 30 |
|
30 |
| -#[bench] |
31 |
| -fn seq_slice_choose_multiple_1_of_1000(b: &mut Bencher) { |
32 |
| - let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); |
33 |
| - let x : &[usize] = &[1; 1000]; |
34 |
| - b.iter(|| { |
35 |
| - x.choose_multiple(&mut rng, 1).cloned().next() |
36 |
| - }) |
37 |
| -} |
38 |
| - |
39 |
| -#[bench] |
40 |
| -fn seq_slice_choose_multiple_10_of_100(b: &mut Bencher) { |
41 |
| - let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); |
42 |
| - let x : &[usize] = &[1; 100]; |
43 |
| - let mut buf = [0; 10]; |
44 |
| - b.iter(|| { |
45 |
| - for (v, slot) in x.choose_multiple(&mut rng, buf.len()).zip(buf.iter_mut()) { |
46 |
| - *slot = *v; |
| 31 | +macro_rules! seq_slice_choose_multiple { |
| 32 | + ($name:ident, $amount:expr, $length:expr) => { |
| 33 | + #[bench] |
| 34 | + fn $name(b: &mut Bencher) { |
| 35 | + let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); |
| 36 | + let x : &[i32] = &[$amount; $length]; |
| 37 | + let mut result = [0i32; $amount]; |
| 38 | + b.iter(|| { |
| 39 | + // Collect full result to prevent unwanted shortcuts getting |
| 40 | + // first element (in case sample_indices returns an iterator). |
| 41 | + for (slot, sample) in result.iter_mut().zip( |
| 42 | + x.choose_multiple(&mut rng, $amount)) { |
| 43 | + *slot = *sample; |
| 44 | + } |
| 45 | + result[$amount-1] |
| 46 | + }) |
47 | 47 | }
|
48 |
| - buf |
49 |
| - }) |
| 48 | + } |
50 | 49 | }
|
51 | 50 |
|
| 51 | +seq_slice_choose_multiple!(seq_slice_choose_multiple_1_of_1000, 1, 1000); |
| 52 | +seq_slice_choose_multiple!(seq_slice_choose_multiple_950_of_1000, 950, 1000); |
| 53 | +seq_slice_choose_multiple!(seq_slice_choose_multiple_10_of_100, 10, 100); |
| 54 | +seq_slice_choose_multiple!(seq_slice_choose_multiple_90_of_100, 90, 100); |
| 55 | + |
52 | 56 | #[bench]
|
53 | 57 | fn seq_iter_choose_from_100(b: &mut Bencher) {
|
54 | 58 | let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
@@ -78,17 +82,22 @@ fn seq_iter_choose_multiple_fill_10_of_100(b: &mut Bencher) {
|
78 | 82 | }
|
79 | 83 |
|
80 | 84 | macro_rules! sample_indices {
|
81 |
| - ($name:ident, $amount:expr, $length:expr) => { |
| 85 | + ($name:ident, $fn:ident, $amount:expr, $length:expr) => { |
82 | 86 | #[bench]
|
83 | 87 | fn $name(b: &mut Bencher) {
|
84 | 88 | let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
85 | 89 | b.iter(|| {
|
86 |
| - sample_indices(&mut rng, $length, $amount) |
| 90 | + index::$fn(&mut rng, $length, $amount) |
87 | 91 | })
|
88 | 92 | }
|
89 | 93 | }
|
90 | 94 | }
|
91 | 95 |
|
92 |
| -sample_indices!(seq_sample_indices_10_of_1k, 10, 1000); |
93 |
| -sample_indices!(seq_sample_indices_50_of_1k, 50, 1000); |
94 |
| -sample_indices!(seq_sample_indices_100_of_1k, 100, 1000); |
| 96 | +sample_indices!(misc_sample_indices_1_of_1k, sample, 1, 1000); |
| 97 | +sample_indices!(misc_sample_indices_10_of_1k, sample, 10, 1000); |
| 98 | +sample_indices!(misc_sample_indices_100_of_1k, sample, 100, 1000); |
| 99 | +sample_indices!(misc_sample_indices_100_of_1M, sample, 100, 1000_000); |
| 100 | +sample_indices!(misc_sample_indices_100_of_1G, sample, 100, 1000_000_000); |
| 101 | +sample_indices!(misc_sample_indices_200_of_1G, sample, 200, 1000_000_000); |
| 102 | +sample_indices!(misc_sample_indices_400_of_1G, sample, 400, 1000_000_000); |
| 103 | +sample_indices!(misc_sample_indices_600_of_1G, sample, 600, 1000_000_000); |
0 commit comments