summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fastrand/benches/bench.rs
blob: 4b882b74f99f1e9977babf646109f0ffc44e575f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#![feature(test)]

extern crate test;

use rand::prelude::*;
use test::Bencher;
use wyhash::WyRng;

#[bench]
fn shuffle_wyhash(b: &mut Bencher) {
    let mut rng = WyRng::from_rng(thread_rng()).unwrap();
    let mut x = (0..100).collect::<Vec<usize>>();
    b.iter(|| {
        x.shuffle(&mut rng);
        x[0]
    })
}

#[bench]
fn shuffle_fastrand(b: &mut Bencher) {
    let rng = fastrand::Rng::new();
    let mut x = (0..100).collect::<Vec<usize>>();
    b.iter(|| {
        rng.shuffle(&mut x);
        x[0]
    })
}

#[bench]
fn u8_wyhash(b: &mut Bencher) {
    let mut rng = WyRng::from_rng(thread_rng()).unwrap();
    b.iter(|| {
        let mut sum = 0u8;
        for _ in 0..10_000 {
            sum = sum.wrapping_add(rng.gen::<u8>());
        }
        sum
    })
}

#[bench]
fn u8_fastrand(b: &mut Bencher) {
    let rng = fastrand::Rng::new();
    b.iter(|| {
        let mut sum = 0u8;
        for _ in 0..10_000 {
            sum = sum.wrapping_add(rng.u8(..));
        }
        sum
    })
}

#[bench]
fn u32_wyhash(b: &mut Bencher) {
    let mut rng = WyRng::from_rng(thread_rng()).unwrap();
    b.iter(|| {
        let mut sum = 0u32;
        for _ in 0..10_000 {
            sum = sum.wrapping_add(rng.gen::<u32>());
        }
        sum
    })
}

#[bench]
fn u32_fastrand(b: &mut Bencher) {
    let rng = fastrand::Rng::new();
    b.iter(|| {
        let mut sum = 0u32;
        for _ in 0..10_000 {
            sum = sum.wrapping_add(rng.u32(..));
        }
        sum
    })
}

#[bench]
fn fill(b: &mut Bencher) {
    let rng = fastrand::Rng::new();
    b.iter(|| {
        // Pick a size that isn't divisble by 8.
        let mut bytes = [0u8; 367];
        rng.fill(&mut bytes);
        bytes
    })
}

#[bench]
fn fill_naive(b: &mut Bencher) {
    let rng = fastrand::Rng::new();
    b.iter(|| {
        let mut bytes = [0u8; 367];
        for item in &mut bytes {
            *item = rng.u8(..);
        }
        bytes
    })
}