Debian Codegraph @

debian/missing-sources/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/utf8/TinyAsciiStr", |b| {
19 b.iter(|| {
20 for s in STRINGS_4 {
21 let _: TinyAsciiStr<4> =
22 TinyAsciiStr::try_from_utf8(black_box(s.as_bytes())).unwrap();
23 let _: TinyAsciiStr<8> =
24 TinyAsciiStr::try_from_utf8(black_box(s.as_bytes())).unwrap();
25 let _: TinyAsciiStr<16> =
26 TinyAsciiStr::try_from_utf8(black_box(s.as_bytes())).unwrap();
27 }
28 for s in STRINGS_8 {
29 let _: TinyAsciiStr<8> =
30 TinyAsciiStr::try_from_utf8(black_box(s.as_bytes())).unwrap();
31 let _: TinyAsciiStr<16> =
32 TinyAsciiStr::try_from_utf8(black_box(s.as_bytes())).unwrap();
33 }
34 for s in STRINGS_16 {
35 let _: TinyAsciiStr<16> =
36 TinyAsciiStr::try_from_utf8(black_box(s.as_bytes())).unwrap();
37 }
38 });
39 });
40
41 let strings_4_utf16: Vec<Vec<u16>> = STRINGS_4
42 .iter()
43 .map(|s| s.encode_utf16().collect())
44 .collect();
45
46 let strings_8_utf16: Vec<Vec<u16>> = STRINGS_8
47 .iter()
48 .map(|s| s.encode_utf16().collect())
49 .collect();
50
51 let strings_16_utf16: Vec<Vec<u16>> = STRINGS_16
52 .iter()
53 .map(|s| s.encode_utf16().collect())
54 .collect();
55
56 g.bench_function("construct/utf16/TinyAsciiStr", |b| {
57 b.iter(|| {
58 for s in strings_4_utf16.iter() {
59 let _: TinyAsciiStr<4> = TinyAsciiStr::try_from_utf16(black_box(s)).unwrap();
60 let _: TinyAsciiStr<8> = TinyAsciiStr::try_from_utf16(black_box(s)).unwrap();
61 let _: TinyAsciiStr<16> = TinyAsciiStr::try_from_utf16(black_box(s)).unwrap();
62 }
63 for s in strings_8_utf16.iter() {
64 let _: TinyAsciiStr<8> = TinyAsciiStr::try_from_utf16(black_box(s)).unwrap();
65 let _: TinyAsciiStr<16> = TinyAsciiStr::try_from_utf16(black_box(s)).unwrap();
66 }
67 for s in strings_16_utf16.iter() {
68 let _: TinyAsciiStr<16> = TinyAsciiStr::try_from_utf16(black_box(s)).unwrap();
69 }
70 });
71 });
72
73 let parsed_ascii_4: Vec<TinyAsciiStr<4>> = STRINGS_4
74 .iter()
75 .map(|s| s.parse::<TinyAsciiStr<4>>().unwrap())
76 .collect();
77 let parsed_ascii_8: Vec<TinyAsciiStr<8>> = STRINGS_4
78 .iter()
79 .chain(STRINGS_8)
80 .map(|s| s.parse::<TinyAsciiStr<8>>().unwrap())
81 .collect();
82 let parsed_ascii_16: Vec<TinyAsciiStr<16>> = STRINGS_4
83 .iter()
84 .chain(STRINGS_8)
85 .chain(STRINGS_16)
86 .map(|s| s.parse::<TinyAsciiStr<16>>().unwrap())
87 .collect();
88
89 g.bench_function("read/TinyAsciiStr", |b| {
90 b.iter(|| {
91 let mut collector: usize = 0;
92 for t in black_box(&parsed_ascii_4) {
93 let s: &str = t;
94 collector += s.bytes().map(usize::from).sum::<usize>();
95 }
96 for t in black_box(&parsed_ascii_8) {
97 let s: &str = t;
98 collector += s.bytes().map(usize::from).sum::<usize>();
99 }
100 for t in black_box(&parsed_ascii_16) {
101 let s: &str = t;
102 collector += s.bytes().map(usize::from).sum::<usize>();
103 }
104 collector
105 });
106 });
107
108 g.bench_function("compare/TinyAsciiStr", |b| {
109 b.iter(|| {
110 let mut collector: usize = 0;
111 for ts in black_box(&parsed_ascii_4).windows(2) {
112 let o = ts[0].cmp(&ts[1]);
113 collector ^= o as usize;
114 }
115 for ts in black_box(&parsed_ascii_8).windows(2) {
116 let o = ts[0].cmp(&ts[1]);
117 collector ^= o as usize;
118 }
119 for ts in black_box(&parsed_ascii_16).windows(2) {
120 let o = ts[0].cmp(&ts[1]);
121 collector ^= o as usize;
122 }
123 collector
124 });
125 });
126 }
127
128 criterion_group!(benches, overview,);
129 criterion_main!(benches);