Skip to content
Permalink
Browse files

Attempt to split generated u64 values into two u32

This *reduces* performance in the mt19937_64::benchmark_gen_624_u32 benchmark
in my tests.

Pushing for comment, DO NOT MERGE.
  • Loading branch information...
dhardy committed Jul 25, 2017
1 parent 2545958 commit f7844f37e6b1f59c218d3b010eec72905c9cbd38
Showing with 9 additions and 1 deletion.
  1. +9 −1 src/mt19937_64.rs
@@ -27,12 +27,14 @@ const LM: Wrapping<u64> = Wrapping( 0x7FFFFFFF); // Least significant 31
#[allow(non_camel_case_types)]
#[derive(Copy)]
pub struct MT19937_64 {
next_u32: Option<u32>,
idx: usize,
state: [Wrapping<u64>; NN],
}

const UNINITIALIZED: MT19937_64 = MT19937_64 {
idx: 0,
next_u32: None,
state: [Wrapping(0); NN]
};

@@ -92,7 +94,13 @@ impl<'a> SeedableRng<&'a [u64]> for MT19937_64 {
impl Rng for MT19937_64 {
#[inline]
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
if let Some(n) = self.next_u32.take() {
n
} else {
let n64 = self.next_u64();
self.next_u32 = Some((n64 >> 32) as u32);
n64 as u32
}
}

#[inline]

1 comment on commit f7844f3

@dhardy

This comment has been minimized.

Copy link
Owner Author

dhardy commented on f7844f3 Jul 25, 2017

Without this change:

test mt19937::benchmark_gen_312_u64        ... bench:       1,314 ns/iter (+/- 37)
test mt19937::benchmark_gen_624_u32        ... bench:       1,764 ns/iter (+/- 39)
test mt19937_64::benchmark_gen_312_u64     ... bench:         865 ns/iter (+/- 25)
test mt19937_64::benchmark_gen_624_u32     ... bench:       1,742 ns/iter (+/- 177)

With this change:

test mt19937::benchmark_gen_312_u64        ... bench:       1,294 ns/iter (+/- 77)
test mt19937::benchmark_gen_624_u32        ... bench:       1,784 ns/iter (+/- 148)
test mt19937_64::benchmark_gen_312_u64     ... bench:         888 ns/iter (+/- 39)
test mt19937_64::benchmark_gen_624_u32     ... bench:       2,422 ns/iter (+/- 24)
Please sign in to comment.
You can’t perform that action at this time.