Added benchmarks to rrand sub-crate.

Also misc naming cleanup.
This commit is contained in:
Nathan Vegdahl 2022-08-04 11:51:38 -07:00
parent a12de4c3d7
commit e1c983a7e6
6 changed files with 91 additions and 16 deletions

4
Cargo.lock generated
View File

@ -569,6 +569,10 @@ dependencies = [
[[package]] [[package]]
name = "rrand" name = "rrand"
version = "0.1.0" version = "0.1.0"
dependencies = [
"bencher",
"rand 0.6.5",
]
[[package]] [[package]]
name = "rustc-serialize" name = "rustc-serialize"

View File

@ -5,7 +5,7 @@ use std::{
mem::MaybeUninit, mem::MaybeUninit,
}; };
use rrand::mix_seed_u64; use rrand::mix64_seed;
use crate::lerp::{lerp_slice, Lerp}; use crate::lerp::{lerp_slice, Lerp};
@ -208,7 +208,7 @@ where
let mut seed = n as u64; let mut seed = n as u64;
loop { loop {
let i = left + (mix_seed_u64(right as u64, seed) as usize % (right - left)); let i = left + (mix64_seed(right as u64, seed) as usize % (right - left));
slc.swap(i, right - 1); slc.swap(i, right - 1);
let ii = left + { let ii = left + {

View File

@ -513,11 +513,11 @@ fn trace_camera_light_path(
/// Generates Owen-scrambled, padded Sobol samples. /// Generates Owen-scrambled, padded Sobol samples.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
struct SampleGen { struct SampleGen {
sample_i: u32,
sample_i_shuffled_rev: u32,
padding_rng: rrand::Rng, padding_rng: rrand::Rng,
dimension_rng: rrand::Rng, dimension_rng: rrand::Rng,
padding_seed: u32, padding_seed: u32,
sample_i: u32,
sample_i_shuffled_rev: u32,
dimension_i: u32, dimension_i: u32,
} }
@ -526,10 +526,10 @@ impl SampleGen {
let mut gen = Self { let mut gen = Self {
sample_i: sample_i, sample_i: sample_i,
sample_i_shuffled_rev: 0, sample_i_shuffled_rev: 0,
padding_rng: rrand::Rng::new(seed as u64),
dimension_rng: rrand::Rng::new(0),
padding_seed: 0, padding_seed: 0,
dimension_i: 0, dimension_i: 0,
padding_rng: rrand::Rng::new(rrand::mix_u64(seed as u64)),
dimension_rng: rrand::Rng::new(0),
}; };
gen.inc_padding(); gen.inc_padding();

View File

@ -3,6 +3,10 @@ name = "rrand"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dev-dependencies]
bencher = "0.1.5"
rand = "0.6"
[dependencies] [[bench]]
name = "bench"
harness = false

View File

@ -0,0 +1,67 @@
use bencher::{benchmark_group, benchmark_main, black_box, Bencher};
use rrand::{mix32, mix32_seed, mix64, mix64_seed, Rng};
//----
fn rng_u32_100000(bench: &mut Bencher) {
bench.iter(|| {
let mut rng = Rng::new(black_box(0));
for _ in 0..100000 {
black_box(rng.u32());
}
});
}
fn rng_u64_100000(bench: &mut Bencher) {
bench.iter(|| {
let mut rng = Rng::new(black_box(0));
for _ in 0..100000 {
black_box(rng.u64());
}
});
}
fn mix32_100000(bench: &mut Bencher) {
bench.iter(|| {
for i in 0..100000 {
black_box(mix32(black_box(i)));
}
});
}
fn mix64_100000(bench: &mut Bencher) {
bench.iter(|| {
for i in 0..100000 {
black_box(mix64(black_box(i)));
}
});
}
fn mix32_seed_100000(bench: &mut Bencher) {
bench.iter(|| {
for i in 0..100000 {
black_box(mix32_seed(black_box(i), black_box(0)));
}
});
}
fn mix64_seed_100000(bench: &mut Bencher) {
bench.iter(|| {
for i in 0..100000 {
black_box(mix64_seed(black_box(i), black_box(0)));
}
});
}
//----
benchmark_group!(
benches,
rng_u32_100000,
rng_u64_100000,
mix32_100000,
mix64_100000,
mix32_seed_100000,
mix64_seed_100000,
);
benchmark_main!(benches);

View File

@ -67,7 +67,7 @@ impl Rng {
/// Scrambles the input number to produce a different deterministic /// Scrambles the input number to produce a different deterministic
/// "random" number. /// "random" number.
#[inline(always)] #[inline(always)]
pub fn mix_u32(mut n: u32) -> u32 { pub fn mix32(mut n: u32) -> u32 {
// From https://github.com/skeeto/hash-prospector // From https://github.com/skeeto/hash-prospector
n ^= n >> 16; n ^= n >> 16;
n = n.wrapping_mul(0x21f0aaad); n = n.wrapping_mul(0x21f0aaad);
@ -82,13 +82,13 @@ pub fn mix_u32(mut n: u32) -> u32 {
/// A fast seedable 32-bit mixing function. /// A fast seedable 32-bit mixing function.
/// ///
/// Same as `mix_u32()` but takes a seed. /// Same as `mix32()` but takes a seed.
#[inline(always)] #[inline(always)]
pub fn mix_seed_u32(n: u32, seed: u32) -> u32 { pub fn mix32_seed(n: u32, seed: u32) -> u32 {
// We rotate the bits of `seed` so it's unlikely to interact with `n` // We rotate the bits of `seed` so it's unlikely to interact with `n`
// in bad ways if they're both e.g. incrementing. The particular // in bad ways if they're both e.g. incrementing. The particular
// rotation constant used here isn't special. // rotation constant used here isn't special.
mix_u32(n ^ seed.rotate_left(23)) mix32(n ^ seed.rotate_left(23))
} }
/// A fast 64-bit mixing function. /// A fast 64-bit mixing function.
@ -96,7 +96,7 @@ pub fn mix_seed_u32(n: u32, seed: u32) -> u32 {
/// Scrambles the input number to produce a different deterministic /// Scrambles the input number to produce a different deterministic
/// "random" number. /// "random" number.
#[inline(always)] #[inline(always)]
pub fn mix_u64(mut n: u64) -> u64 { pub fn mix64(mut n: u64) -> u64 {
// From https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html // From https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
n ^= n >> 30; n ^= n >> 30;
n = n.wrapping_mul(0xbf58476d1ce4e5b9); n = n.wrapping_mul(0xbf58476d1ce4e5b9);
@ -111,11 +111,11 @@ pub fn mix_u64(mut n: u64) -> u64 {
/// A fast seedable 64-bit mixing function. /// A fast seedable 64-bit mixing function.
/// ///
/// Same as `mix_u64()` but takes a seed. /// Same as `mix64()` but takes a seed.
#[inline(always)] #[inline(always)]
pub fn mix_seed_u64(n: u64, seed: u64) -> u64 { pub fn mix64_seed(n: u64, seed: u64) -> u64 {
// We rotate the bits of `seed` so it's unlikely to interact with `n` // We rotate the bits of `seed` so it's unlikely to interact with `n`
// in bad ways if they're both e.g. incrementing. The particular // in bad ways if they're both e.g. incrementing. The particular
// rotation constant used here isn't special. // rotation constant used here isn't special.
mix_u64(n ^ seed.rotate_left(47)) mix64(n ^ seed.rotate_left(47))
} }