]> git.lizzy.rs Git - rust.git/blob - library/core/benches/str/char_count.rs
Rollup merge of #92715 - chordtoll:empty-string, r=davidtwco
[rust.git] / library / core / benches / str / char_count.rs
1 use super::corpora::*;
2 use test::{black_box, Bencher};
3
4 macro_rules! define_benches {
5     ($( fn $name: ident($arg: ident: &str) $body: block )+) => {
6         define_benches!(mod en_tiny, en::TINY, $($name $arg $body)+);
7         define_benches!(mod en_small, en::SMALL, $($name $arg $body)+);
8         define_benches!(mod en_medium, en::MEDIUM, $($name $arg $body)+);
9         define_benches!(mod en_large, en::LARGE, $($name $arg $body)+);
10         define_benches!(mod en_huge, en::HUGE, $($name $arg $body)+);
11
12         define_benches!(mod zh_tiny, zh::TINY, $($name $arg $body)+);
13         define_benches!(mod zh_small, zh::SMALL, $($name $arg $body)+);
14         define_benches!(mod zh_medium, zh::MEDIUM, $($name $arg $body)+);
15         define_benches!(mod zh_large, zh::LARGE, $($name $arg $body)+);
16         define_benches!(mod zh_huge, zh::HUGE, $($name $arg $body)+);
17
18         define_benches!(mod ru_tiny, ru::TINY, $($name $arg $body)+);
19         define_benches!(mod ru_small, ru::SMALL, $($name $arg $body)+);
20         define_benches!(mod ru_medium, ru::MEDIUM, $($name $arg $body)+);
21         define_benches!(mod ru_large, ru::LARGE, $($name $arg $body)+);
22         define_benches!(mod ru_huge, ru::HUGE, $($name $arg $body)+);
23
24         define_benches!(mod emoji_tiny, emoji::TINY, $($name $arg $body)+);
25         define_benches!(mod emoji_small, emoji::SMALL, $($name $arg $body)+);
26         define_benches!(mod emoji_medium, emoji::MEDIUM, $($name $arg $body)+);
27         define_benches!(mod emoji_large, emoji::LARGE, $($name $arg $body)+);
28         define_benches!(mod emoji_huge, emoji::HUGE, $($name $arg $body)+);
29     };
30     (mod $mod_name: ident, $input: expr, $($name: ident $arg: ident $body: block)+) => {
31         mod $mod_name {
32             use super::*;
33             $(
34                 #[bench]
35                 fn $name(bencher: &mut Bencher) {
36                     let input = $input;
37                     bencher.bytes = input.len() as u64;
38                     let mut input_s = input.to_string();
39                     bencher.iter(|| {
40                         let $arg: &str = &black_box(&mut input_s);
41                         black_box($body)
42                     })
43                 }
44             )+
45         }
46     };
47 }
48
49 define_benches! {
50     fn case00_libcore(s: &str) {
51         libcore(s)
52     }
53
54     fn case01_filter_count_cont_bytes(s: &str) {
55         filter_count_cont_bytes(s)
56     }
57
58     fn case02_iter_increment(s: &str) {
59         iterator_increment(s)
60     }
61
62     fn case03_manual_char_len(s: &str) {
63         manual_char_len(s)
64     }
65 }
66
67 fn libcore(s: &str) -> usize {
68     s.chars().count()
69 }
70
71 #[inline]
72 fn utf8_is_cont_byte(byte: u8) -> bool {
73     (byte as i8) < -64
74 }
75
76 fn filter_count_cont_bytes(s: &str) -> usize {
77     s.as_bytes().iter().filter(|&&byte| !utf8_is_cont_byte(byte)).count()
78 }
79
80 fn iterator_increment(s: &str) -> usize {
81     let mut c = 0;
82     for _ in s.chars() {
83         c += 1;
84     }
85     c
86 }
87
88 fn manual_char_len(s: &str) -> usize {
89     let s = s.as_bytes();
90     let mut c = 0;
91     let mut i = 0;
92     let l = s.len();
93     while i < l {
94         let b = s[i];
95         if b < 0x80 {
96             i += 1;
97         } else if b < 0xe0 {
98             i += 2;
99         } else if b < 0xf0 {
100             i += 3;
101         } else {
102             i += 4;
103         }
104         c += 1;
105     }
106     c
107 }