summaryrefslogtreecommitdiffstats
path: root/vendor/btoi/benches/benches.rs
blob: 51e0921bc5729a30f0e76f4a931d51c77fd7759c (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
#[macro_use]
extern crate bencher;
extern crate btoi;

use bencher::{black_box, Bencher};

fn bench_btou(b: &mut Bencher) {
    b.iter(|| assert_eq!(Ok(123_456_789u32), btoi::btou(black_box(b"123456789"))));
}

fn bench_btou_saturating(b: &mut Bencher) {
    b.iter(|| {
        assert_eq!(
            Ok(123_456_789u32),
            btoi::btou_saturating(black_box(b"123456789"))
        )
    });
}

fn bench_small_btou(b: &mut Bencher) {
    b.iter(|| assert_eq!(Ok(42u8), btoi::btou(black_box(b"42"))));
}

fn bench_small_btou_saturating(b: &mut Bencher) {
    b.iter(|| assert_eq!(Ok(255u8), btoi::btou_saturating(black_box(b"256"))));
}

fn bench_btoi(b: &mut Bencher) {
    b.iter(|| assert_eq!(Ok(-123_456_789i32), btoi::btoi(black_box(b"-123456789"))));
}

fn bench_from_str(b: &mut Bencher) {
    fn btou_from_str(s: &[u8]) -> Option<u32> {
        ::std::str::from_utf8(s).ok().and_then(|s| s.parse().ok())
    }

    b.iter(|| assert_eq!(Some(123_456_789), btou_from_str(black_box(b"123456789"))));
}

fn bench_from_str_unchecked(b: &mut Bencher) {
    unsafe fn btou_from_str_unchecked(s: &[u8]) -> Option<u32> {
        ::std::str::from_utf8_unchecked(s).parse().ok()
    }

    b.iter(|| {
        assert_eq!(Some(123_456_789), unsafe {
            btou_from_str_unchecked(black_box(b"123456789"))
        })
    });
}

fn bench_small_from_str(b: &mut Bencher) {
    fn btou_from_str(s: &[u8]) -> Option<u8> {
        ::std::str::from_utf8(s).ok().and_then(|s| s.parse().ok())
    }

    b.iter(|| assert_eq!(Some(42u8), btou_from_str(black_box(b"42"))));
}

fn bench_small_from_str_unchecked(b: &mut Bencher) {
    unsafe fn btou_from_str_unchecked(s: &[u8]) -> Option<u8> {
        ::std::str::from_utf8_unchecked(s).parse().ok()
    }

    b.iter(|| {
        assert_eq!(Some(42u8), unsafe {
            btou_from_str_unchecked(black_box(b"42"))
        })
    });
}

benchmark_group!(
    benches,
    bench_btou,
    bench_btou_saturating,
    bench_btoi,
    bench_from_str,
    bench_from_str_unchecked,
    bench_small_btou,
    bench_small_btou_saturating,
    bench_small_from_str,
    bench_small_from_str_unchecked
);

benchmark_main!(benches);