Skip to content

Commit

Permalink
Fix slice's benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
kzys committed Oct 11, 2018
1 parent f42c510 commit f930087
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/liballoc/benches/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::mem;
use std::ptr;

use rand::{Rng, SeedableRng, XorShiftRng};
use rand::distributions::{Standard, Alphanumeric};
use test::{Bencher, black_box};

#[bench]
Expand Down Expand Up @@ -192,18 +193,20 @@ fn gen_descending(len: usize) -> Vec<u64> {
(0..len as u64).rev().collect()
}

const SEED: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

fn gen_random(len: usize) -> Vec<u64> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
rng.gen_iter::<u64>().take(len).collect()
let mut rng = XorShiftRng::from_seed(SEED);
rng.sample_iter(&Standard).take(len).collect()
}

fn gen_random_bytes(len: usize) -> Vec<u8> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
rng.gen_iter::<u8>().take(len).collect()
let mut rng = XorShiftRng::from_seed(SEED);
rng.sample_iter(&Standard).take(len).collect()
}

fn gen_mostly_ascending(len: usize) -> Vec<u64> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
let mut rng = XorShiftRng::from_seed(SEED);
let mut v = gen_ascending(len);
for _ in (0usize..).take_while(|x| x * x <= len) {
let x = rng.gen::<usize>() % len;
Expand All @@ -214,7 +217,7 @@ fn gen_mostly_ascending(len: usize) -> Vec<u64> {
}

fn gen_mostly_descending(len: usize) -> Vec<u64> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
let mut rng = XorShiftRng::from_seed(SEED);
let mut v = gen_descending(len);
for _ in (0usize..).take_while(|x| x * x <= len) {
let x = rng.gen::<usize>() % len;
Expand All @@ -225,18 +228,18 @@ fn gen_mostly_descending(len: usize) -> Vec<u64> {
}

fn gen_strings(len: usize) -> Vec<String> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
let mut rng = XorShiftRng::from_seed(SEED);
let mut v = vec![];
for _ in 0..len {
let n = rng.gen::<usize>() % 20 + 1;
v.push(rng.gen_ascii_chars().take(n).collect());
v.push(rng.sample_iter(&Alphanumeric).take(n).collect());
}
v
}

fn gen_big_random(len: usize) -> Vec<[u64; 16]> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
rng.gen_iter().map(|x| [x; 16]).take(len).collect()
let mut rng = XorShiftRng::from_seed(SEED);
rng.sample_iter(&Standard).map(|x| [x; 16]).take(len).collect()
}

macro_rules! sort {
Expand Down

0 comments on commit f930087

Please sign in to comment.