-
-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sequence functionality: API revisions #483
Changes from all commits
81b7f41
f64ce3a
545895b
0de02f1
b20944d
c083d58
1b118c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
extern crate rand; | ||
|
||
use test::Bencher; | ||
|
||
use rand::prelude::*; | ||
use rand::seq::*; | ||
|
||
#[bench] | ||
fn seq_shuffle_100(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &mut [usize] = &mut [1; 100]; | ||
b.iter(|| { | ||
x.shuffle(&mut rng); | ||
x[0] | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn seq_slice_choose_1_of_1000(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &[usize] = &[1; 1000]; | ||
b.iter(|| { | ||
x.choose(&mut rng) | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn seq_slice_choose_multiple_1_of_1000(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &[usize] = &[1; 1000]; | ||
b.iter(|| { | ||
x.choose_multiple(&mut rng, 1).cloned().next() | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn seq_slice_choose_multiple_10_of_100(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &[usize] = &[1; 100]; | ||
let mut buf = [0; 10]; | ||
b.iter(|| { | ||
for (v, slot) in x.choose_multiple(&mut rng, buf.len()).zip(buf.iter_mut()) { | ||
*slot = *v; | ||
} | ||
buf | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn seq_iter_choose_from_100(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &[usize] = &[1; 100]; | ||
b.iter(|| { | ||
x.iter().cloned().choose(&mut rng) | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn seq_iter_choose_multiple_10_of_100(b: &mut Bencher) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this function need to be flagged with #[cfg(feature = "alloc")]? Or does #[bench] only work in std builds anyway? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, cool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &[usize] = &[1; 100]; | ||
b.iter(|| { | ||
x.iter().cloned().choose_multiple(&mut rng, 10) | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn seq_iter_choose_multiple_fill_10_of_100(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
let x : &[usize] = &[1; 100]; | ||
let mut buf = [0; 10]; | ||
b.iter(|| { | ||
x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf) | ||
}) | ||
} | ||
|
||
macro_rules! sample_indices { | ||
($name:ident, $amount:expr, $length:expr) => { | ||
#[bench] | ||
fn $name(b: &mut Bencher) { | ||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); | ||
b.iter(|| { | ||
sample_indices(&mut rng, $length, $amount) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
sample_indices!(seq_sample_indices_10_of_1k, 10, 1000); | ||
sample_indices!(seq_sample_indices_50_of_1k, 50, 1000); | ||
sample_indices!(seq_sample_indices_100_of_1k, 100, 1000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this does measure both the time to call
choose_multiple
and the time to write intobuf
, whereas we only really care about the former.You could do something like
x.choose_multiple(&mut rng, 10).sum::<usize>()
.But I kind'a worry that the compiler would be smart enough to realize that since all values in
x
are1
, that it might optimize to simply returning10
. That could be fixed by simply changing one of the values in x though.But since the writes are consecutive, they should be fast and maybe we shouldn't worry about this? I don't have strong feelings either way.
As a complete aside, it'd be really great if there was a version of
Bencher::iter
which you could return anIterator
to, and it would consume the iterator. But I'm not sure if there's any appetite for changing that API since apparently the idea is to deprecate it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is important. (I have tested that writing to a buffer is faster than collecting into a vector.)