Skip to content

Commit

Permalink
Add benchmarks: generate 1k block
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Mar 6, 2018
1 parent 5246818 commit 906887b
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate rand;

use test::{black_box, Bencher};

use rand::{Rng, weak_rng};
use rand::{Rng, RngCore, weak_rng};
use rand::seq::*;

#[bench]
Expand Down Expand Up @@ -60,3 +60,56 @@ macro_rules! sample_indices {
sample_indices!(misc_sample_indices_10_of_1k, 10, 1000);
sample_indices!(misc_sample_indices_50_of_1k, 50, 1000);
sample_indices!(misc_sample_indices_100_of_1k, 100, 1000);

#[bench]
fn gen_1k_iter_repeat(b: &mut Bencher) {
use std::iter;
let mut rng = weak_rng();
b.iter(|| {
let v: Vec<u32> = iter::repeat(()).map(|()| rng.next_u32()).take(256).collect();
black_box(v);
});
b.bytes = 1024;
}

#[bench]
fn gen_1k_gen_iter(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| {
let v: Vec<u32> = rng.gen_iter().take(256).collect();
black_box(v);
});
b.bytes = 1024;
}

#[bench]
fn gen_1k_iter1(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| {
let v: Vec<u32> = rng.iter().take(256).map(|rng| rng.next_u32()).collect();
black_box(v);
});
b.bytes = 1024;
}

#[bench]
fn gen_1k_iter2(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| {
let v: Vec<u32> = rng.iter().map(|rng| rng.next_u32()).take(256).collect();
black_box(v);
});
b.bytes = 1024;
}

#[bench]
fn gen_1k_fill(b: &mut Bencher) {
use std::mem::transmute;
let mut rng = weak_rng();
let mut buf = [0u32; 256];
b.iter(|| {
rng.fill_bytes(unsafe{ transmute(&mut buf[..]) });
black_box(buf);
});
b.bytes = 1024;
}

0 comments on commit 906887b

Please sign in to comment.