Created
April 2, 2016 17:49
-
-
Save jorendorff/8b827e27ace2e15328089668aeb721b3 to your computer and use it in GitHub Desktop.
randspeed/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(test)] | |
| /* | |
| * main.rs - Clueless random number generation benchmark | |
| */ | |
| extern crate rand; | |
| extern crate test; | |
| use rand::{random, thread_rng, XorShiftRng, Isaac64Rng, StdRng, Rng}; | |
| use test::Bencher; | |
| fn main() { | |
| println!("Hello, world!"); | |
| } | |
| #[bench] | |
| fn bench_random(b: &mut Bencher) { | |
| let mut x: f64 = 0.0; | |
| b.iter(|| { | |
| for _ in 0..1000 { | |
| x += random::<f64>(); | |
| } | |
| }); | |
| } | |
| #[bench] | |
| fn bench_thread_rng(b: &mut Bencher) { | |
| let mut x: f64 = 0.0; | |
| let mut rng = thread_rng(); | |
| b.iter(|| { | |
| for _ in 0..1000 { | |
| x += rng.gen::<f64>(); | |
| } | |
| }); | |
| } | |
| #[bench] | |
| fn bench_std_rng(b: &mut Bencher) { | |
| let mut x: f64 = 0.0; | |
| let mut rng = StdRng::new().unwrap(); | |
| b.iter(move || { | |
| for _ in 0..1000 { | |
| x += rng.gen::<f64>(); | |
| } | |
| }); | |
| } | |
| #[bench] | |
| fn bench_isaac_rng(b: &mut Bencher) { | |
| let mut x: f64 = 0.0; | |
| let mut rng = Isaac64Rng::new_unseeded(); | |
| b.iter(move || { | |
| for _ in 0..1000 { | |
| x += rng.gen::<f64>(); | |
| } | |
| }); | |
| } | |
| #[bench] | |
| fn bench_xorshift(b: &mut Bencher) { | |
| let mut x: f64 = 0.0; | |
| let mut rng = XorShiftRng::new_unseeded(); | |
| b.iter(|| { | |
| for _ in 0..1000 { | |
| x += rng.gen::<f64>(); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment