Skip to content

Commit 9b5db4c

Browse files
authored
Merge pull request #479 from dhardy/sample_floyd
seq: use Floyd's combination algorithm to sample indices
2 parents d8bea3a + 19897e5 commit 9b5db4c

File tree

5 files changed

+465
-187
lines changed

5 files changed

+465
-187
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
88

99
You may also find the [Update Guide](UPDATING.md) useful.
1010

11+
## [0.6.0] - Unreleased
12+
13+
### Sequences module
14+
- Optimised and changed return type of the `sample_indices` function. (#479)
15+
1116
## [0.5.4] - 2018-07-11
1217
### Platform support
1318
- Make `OsRng` work via WASM/stdweb for WebWorkers

benches/seq.rs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(test)]
2+
#![allow(non_snake_case)]
23

34
extern crate test;
45
extern crate rand;
@@ -27,28 +28,31 @@ fn seq_slice_choose_1_of_1000(b: &mut Bencher) {
2728
})
2829
}
2930

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+
})
4747
}
48-
buf
49-
})
48+
}
5049
}
5150

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+
5256
#[bench]
5357
fn seq_iter_choose_from_100(b: &mut Bencher) {
5458
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) {
7882
}
7983

8084
macro_rules! sample_indices {
81-
($name:ident, $amount:expr, $length:expr) => {
85+
($name:ident, $fn:ident, $amount:expr, $length:expr) => {
8286
#[bench]
8387
fn $name(b: &mut Bencher) {
8488
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
8589
b.iter(|| {
86-
sample_indices(&mut rng, $length, $amount)
90+
index::$fn(&mut rng, $length, $amount)
8791
})
8892
}
8993
}
9094
}
9195

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);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
#![cfg_attr(feature = "wasm-bindgen", feature(wasm_import_module))]
235235

236236
#[cfg(feature = "std")] extern crate core;
237-
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
237+
#[cfg(all(feature = "alloc", not(feature="std")))] #[macro_use] extern crate alloc;
238238

239239
#[cfg(feature="simd_support")] extern crate packed_simd;
240240

0 commit comments

Comments
 (0)