Skip to content

Instantly share code, notes, and snippets.

@philippkeller
Created March 9, 2017 06:08
Show Gist options
  • Save philippkeller/b378d98e9faa7e94dd4702bc8508d93c to your computer and use it in GitHub Desktop.
Save philippkeller/b378d98e9faa7e94dd4702bc8508d93c to your computer and use it in GitHub Desktop.
Benchmarking libc::random vs. XorShiftRng
#![feature(test)]
#![feature(rand)]
extern crate test;
extern crate rand;
use rand::Rng;
extern "C" {
pub fn random() -> i64;
pub fn srandom(arg1: u32);
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn libc_random(b: &mut Bencher) {
b.iter(|| unsafe{random()});
}
#[bench]
fn xorshift_random(b: &mut Bencher) {
let mut rng = rand::XorShiftRng::new_unseeded();
b.iter(|| rng.next_u64());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment