Debian Codegraph @

third_party/rust/tinystr/benches/overview.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 mod common;
6 use common::*;
7
8 use criterion::black_box;
9 use criterion::criterion_group;
10 use criterion::criterion_main;
11 use criterion::Criterion;
12
13 use tinystr::TinyAsciiStr;
14
15 fn overview(c: &mut Criterion) {
16 let mut g = c.benchmark_group("overview");
17
18 g.bench_function("construct/TinyAsciiStr", |b| {
19 b.iter(|| {
20 for s in STRINGS_4 {
21 let _: TinyAsciiStr<4> = black_box(s).parse().unwrap();
22 let _: TinyAsciiStr<8> = black_box(s).parse().unwrap();
23 let _: TinyAsciiStr<16> = black_box(s).parse().unwrap();
24 }
25 for s in STRINGS_8 {
26 let _: TinyAsciiStr<8> = black_box(s).parse().unwrap();
27 let _: TinyAsciiStr<16> = black_box(s).parse().unwrap();
28 }
29 for s in STRINGS_16 {
30 let _: TinyAsciiStr<16> = black_box(s).parse().unwrap();
31 }
32 });
33 });
34
35 let parsed_ascii_4: Vec<TinyAsciiStr<4>> = STRINGS_4
36 .iter()
37 .map(|s| s.parse::<TinyAsciiStr<4>>().unwrap())
38 .collect();
39 let parsed_ascii_8: Vec<TinyAsciiStr<8>> = STRINGS_4
40 .iter()
41 .chain(STRINGS_8)
42 .map(|s| s.parse::<TinyAsciiStr<8>>().unwrap())
43 .collect();
44 let parsed_ascii_16: Vec<TinyAsciiStr<16>> = STRINGS_4
45 .iter()
46 .chain(STRINGS_8)
47 .chain(STRINGS_16)
48 .map(|s| s.parse::<TinyAsciiStr<16>>().unwrap())
49 .collect();
50
51 g.bench_function("read/TinyAsciiStr", |b| {
52 b.iter(|| {
53 let mut collector: usize = 0;
54 for t in black_box(&parsed_ascii_4) {
55 let s: &str = t;
56 collector += s.bytes().map(usize::from).sum::<usize>();
57 }
58 for t in black_box(&parsed_ascii_8) {
59 let s: &str = t;
60 collector += s.bytes().map(usize::from).sum::<usize>();
61 }
62 for t in black_box(&parsed_ascii_16) {
63 let s: &str = t;
64 collector += s.bytes().map(usize::from).sum::<usize>();
65 }
66 collector
67 });
68 });
69
70 g.bench_function("compare/TinyAsciiStr", |b| {
71 b.iter(|| {
72 let mut collector: usize = 0;
73 for ts in black_box(&parsed_ascii_4).windows(2) {
74 let o = ts[0].cmp(&ts[1]);
75 collector ^= o as usize;
76 }
77 for ts in black_box(&parsed_ascii_8).windows(2) {
78 let o = ts[0].cmp(&ts[1]);
79 collector ^= o as usize;
80 }
81 for ts in black_box(&parsed_ascii_16).windows(2) {
82 let o = ts[0].cmp(&ts[1]);
83 collector ^= o as usize;
84 }
85 collector
86 });
87 });
88 }
89
90 criterion_group!(benches, overview,);
91 criterion_main!(benches);