Skip to content

Commit 3c25baa

Browse files
committed
std::rand: share the benchmark counter among the whole module tree.
1 parent cd50fb3 commit 3c25baa

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

src/libstd/rand/distributions/gamma.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,23 @@ mod bench {
182182
use super::*;
183183
use mem::size_of;
184184
use rand::distributions::IndependentSample;
185-
use rand::StdRng;
185+
use rand::{StdRng, RAND_BENCH_N};
186186
use extra::test::BenchHarness;
187187
use iter::range;
188188
use option::{Some, None};
189189

190-
static N: u64 = 100;
191190

192191
#[bench]
193192
fn bench_gamma_large_shape(bh: &mut BenchHarness) {
194193
let gamma = Gamma::new(10., 1.0);
195194
let mut rng = StdRng::new();
196195

197196
do bh.iter {
198-
for _ in range(0, N) {
197+
for _ in range(0, RAND_BENCH_N) {
199198
gamma.ind_sample(&mut rng);
200199
}
201200
}
202-
bh.bytes = size_of::<f64>() as u64 * N;
201+
bh.bytes = size_of::<f64>() as u64 * RAND_BENCH_N;
203202
}
204203

205204
#[bench]
@@ -208,10 +207,10 @@ mod bench {
208207
let mut rng = StdRng::new();
209208

210209
do bh.iter {
211-
for _ in range(0, N) {
210+
for _ in range(0, RAND_BENCH_N) {
212211
gamma.ind_sample(&mut rng);
213212
}
214213
}
215-
bh.bytes = size_of::<f64>() as u64 * N;
214+
bh.bytes = size_of::<f64>() as u64 * RAND_BENCH_N;
216215
}
217216
}

src/libstd/rand/distributions/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -556,36 +556,34 @@ mod tests {
556556
#[cfg(test)]
557557
mod bench {
558558
use extra::test::BenchHarness;
559-
use rand::*;
559+
use rand::{XorShiftRng, RAND_BENCH_N};
560560
use super::*;
561561
use iter::range;
562562
use option::{Some, None};
563563
use mem::size_of;
564564

565-
static N: u64 = 100;
566-
567565
#[bench]
568566
fn rand_normal(bh: &mut BenchHarness) {
569567
let mut rng = XorShiftRng::new();
570568
let mut normal = Normal::new(-2.71828, 3.14159);
571569

572570
do bh.iter {
573-
for _ in range(0, N) {
571+
for _ in range(0, RAND_BENCH_N) {
574572
normal.sample(&mut rng);
575573
}
576574
}
577-
bh.bytes = size_of::<f64>() as u64 * N;
575+
bh.bytes = size_of::<f64>() as u64 * RAND_BENCH_N;
578576
}
579577
#[bench]
580578
fn rand_exp(bh: &mut BenchHarness) {
581579
let mut rng = XorShiftRng::new();
582580
let mut exp = Exp::new(2.71828 * 3.14159);
583581

584582
do bh.iter {
585-
for _ in range(0, N) {
583+
for _ in range(0, RAND_BENCH_N) {
586584
exp.sample(&mut rng);
587585
}
588586
}
589-
bh.bytes = size_of::<f64>() as u64 * N;
587+
bh.bytes = size_of::<f64>() as u64 * RAND_BENCH_N;
590588
}
591589
}

src/libstd/rand/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -833,58 +833,58 @@ mod test {
833833
}
834834
}
835835

836+
static RAND_BENCH_N: u64 = 100;
837+
836838
#[cfg(test)]
837839
mod bench {
838840
use extra::test::BenchHarness;
839-
use rand::*;
841+
use rand::{XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng, RAND_BENCH_N};
840842
use mem::size_of;
841843
use iter::range;
842844
use option::{Some, None};
843845

844-
static N: u64 = 100;
845-
846846
#[bench]
847847
fn rand_xorshift(bh: &mut BenchHarness) {
848848
let mut rng = XorShiftRng::new();
849849
do bh.iter {
850-
for _ in range(0, N) {
850+
for _ in range(0, RAND_BENCH_N) {
851851
rng.gen::<uint>();
852852
}
853853
}
854-
bh.bytes = size_of::<uint>() as u64 * N;
854+
bh.bytes = size_of::<uint>() as u64 * RAND_BENCH_N;
855855
}
856856

857857
#[bench]
858858
fn rand_isaac(bh: &mut BenchHarness) {
859859
let mut rng = IsaacRng::new();
860860
do bh.iter {
861-
for _ in range(0, N) {
861+
for _ in range(0, RAND_BENCH_N) {
862862
rng.gen::<uint>();
863863
}
864864
}
865-
bh.bytes = size_of::<uint>() as u64 * N;
865+
bh.bytes = size_of::<uint>() as u64 * RAND_BENCH_N;
866866
}
867867

868868
#[bench]
869869
fn rand_isaac64(bh: &mut BenchHarness) {
870870
let mut rng = Isaac64Rng::new();
871871
do bh.iter {
872-
for _ in range(0, N) {
872+
for _ in range(0, RAND_BENCH_N) {
873873
rng.gen::<uint>();
874874
}
875875
}
876-
bh.bytes = size_of::<uint>() as u64 * N;
876+
bh.bytes = size_of::<uint>() as u64 * RAND_BENCH_N;
877877
}
878878

879879
#[bench]
880880
fn rand_std(bh: &mut BenchHarness) {
881881
let mut rng = StdRng::new();
882882
do bh.iter {
883-
for _ in range(0, N) {
883+
for _ in range(0, RAND_BENCH_N) {
884884
rng.gen::<uint>();
885885
}
886886
}
887-
bh.bytes = size_of::<uint>() as u64 * N;
887+
bh.bytes = size_of::<uint>() as u64 * RAND_BENCH_N;
888888
}
889889

890890
#[bench]

0 commit comments

Comments
 (0)