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