Browse Source

Use new rand crate

The crate is now splitted, thus rand becomes rand_core and rand_xorshift
in my case. Because of safe number conversion f64's are used in the
benchmark.
pull/10/head
Andreas Linz 2 years ago
parent
commit
8360ce6069
Signed by: alinz GPG Key ID: 9BF39809C9DA580C
2 changed files with 18 additions and 7 deletions
  1. +2
    -1
      Cargo.toml
  2. +16
    -6
      benches/bench.rs

+ 2
- 1
Cargo.toml View File

@ -12,4 +12,5 @@ readme = "README.md"
[dependencies]
[dev-dependencies]
rand = "0.6.5"
rand_xorshift = "0.1.1"
rand_core = "0.4.0"

+ 16
- 6
benches/bench.rs View File

@ -1,10 +1,12 @@
#![feature(test)]
extern crate rand;
extern crate rand_core;
extern crate rand_xorshift;
extern crate rb;
extern crate test;
use rand::{Rng, XorShiftRng};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use rb::{RbConsumer, RbProducer, SpscRb, RB};
use std::thread;
use test::Bencher;
@ -16,16 +18,24 @@ fn bench_passing_a_1k_buffer_blocking(b: &mut Bencher) {
let rb = SpscRb::new(SIZE);
let producer = rb.producer();
let consumer = rb.consumer();
let mut rng = XorShiftRng::new_unseeded();
let mut rng = XorShiftRng::from_seed([0u8; 16]);
let data = (0..SIZE)
.map(|_| rng.gen_range(-1.0f32, 1.0f32))
.collect::<Vec<f32>>();
.map(|_| rand_float(&mut rng))
.collect::<Vec<f64>>();
thread::spawn(move || loop {
producer.write_blocking(&data).unwrap();
});
let mut buf = [0f32; SIZE];
let mut buf = [0f64; SIZE];
b.iter(|| {
let cnt = consumer.read_blocking(&mut buf).unwrap();
assert_eq!(cnt, SIZE);
});
}
fn rand_float(rng: &mut XorShiftRng) -> f64 {
let r = rng.next_u32();
if r == 0 {
return 0.0;
}
f64::from(u32::max_value() / (r - i32::max_value() as u32))
}

Loading…
Cancel
Save