Skip to content

Instantly share code, notes, and snippets.

@matbesancon
Last active June 20, 2019 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matbesancon/1e5dc12d144d3b91fee7de54d9ed7bce to your computer and use it in GitHub Desktop.
Save matbesancon/1e5dc12d144d3b91fee7de54d9ed7bce to your computer and use it in GitHub Desktop.
Sampling mini-benchmark
# julia v1.1.1
import Random
using Distributions # v0.21.0
import RandomNumbers # v1.3.0
using BenchmarkTools # v0.4.2
function f(µ::T, rng::Random.AbstractRNG) where {T<:Real}
n = 0
Σx = zero(T)
X = Normal(µ, one(T))
while true
Σx += rand(rng, X)
n += 1
tn = Σx / sqrt(n)
if tn > 10.0
break
end
end
return n
end
# reproducing the default from the Rust lib
const rng = RandomNumbers.Xorshifts.Xorshift128Plus((UInt64(42),UInt64(69)))
@benchmark f(0.1, rng)
# BenchmarkTools.Trial:
# memory estimate: 0 bytes
# allocs estimate: 0
# --------------
# minimum time: 11.797 μs (0.00% GC)
# median time: 35.052 μs (0.00% GC)
# mean time: 35.973 μs (0.00% GC)
# maximum time: 96.526 μs (0.00% GC)
# --------------
# samples: 10000
# evals/sample: 1
#![feature(test)]
// probability = "0.15"
// rust 1.34.2, nightly for benchmark
extern crate test;
use test::Bencher;
extern crate probability;
use probability::prelude::*;
fn f<S>(mu : f64, source : &mut S) -> i64 where S: source::Source {
let x = Gaussian::new(mu , 1.0);
let mut n = 0;
let mut sigmax = 0.0;
loop {
sigmax += x.sample(source);
n += 1;
let t = sigmax / (n as f64).sqrt();
if t >= 10.0 {
break;
}
}
return n;
}
#[bench]
fn bench_func(b: &mut Bencher) {
let mut src = source::default();
b.iter(|| {
f(0.1, &mut src)
});
}
// $ cargo +nightly bench
//Finished release [optimized] target(s) in 0.13s
//Running target/release/deps/rust_dist-1a857bca394effe0
//
//running 1 test
//test bench_func ... bench: 63,658 ns/iter (+/- 3,478)
//
//test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment