]> git.lizzy.rs Git - rust.git/blob - library/core/benches/char/methods.rs
Rollup merge of #102178 - RalfJung:bootstrap-backtrace, r=Mark-Simulacrum
[rust.git] / library / core / benches / char / methods.rs
1 use test::Bencher;
2
3 const CHARS: [char; 9] = ['0', 'x', '2', '5', 'A', 'f', '7', '8', '9'];
4 const RADIX: [u32; 5] = [2, 8, 10, 16, 32];
5
6 #[bench]
7 fn bench_to_digit_radix_2(b: &mut Bencher) {
8     b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(2)).min())
9 }
10
11 #[bench]
12 fn bench_to_digit_radix_10(b: &mut Bencher) {
13     b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(10)).min())
14 }
15
16 #[bench]
17 fn bench_to_digit_radix_16(b: &mut Bencher) {
18     b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(16)).min())
19 }
20
21 #[bench]
22 fn bench_to_digit_radix_36(b: &mut Bencher) {
23     b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(36)).min())
24 }
25
26 #[bench]
27 fn bench_to_digit_radix_var(b: &mut Bencher) {
28     b.iter(|| {
29         CHARS
30             .iter()
31             .cycle()
32             .zip(RADIX.iter().cycle())
33             .take(10_000)
34             .map(|(c, radix)| c.to_digit(*radix))
35             .min()
36     })
37 }
38
39 #[bench]
40 fn bench_to_ascii_uppercase(b: &mut Bencher) {
41     b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_uppercase()).min())
42 }
43
44 #[bench]
45 fn bench_to_ascii_lowercase(b: &mut Bencher) {
46     b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_lowercase()).min())
47 }
48
49 #[bench]
50 fn bench_ascii_mix_to_uppercase(b: &mut Bencher) {
51     b.iter(|| (0..=255).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
52 }
53
54 #[bench]
55 fn bench_ascii_mix_to_lowercase(b: &mut Bencher) {
56     b.iter(|| (0..=255).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
57 }
58
59 #[bench]
60 fn bench_ascii_char_to_uppercase(b: &mut Bencher) {
61     b.iter(|| (0..=127).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
62 }
63
64 #[bench]
65 fn bench_ascii_char_to_lowercase(b: &mut Bencher) {
66     b.iter(|| (0..=127).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
67 }
68
69 #[bench]
70 fn bench_non_ascii_char_to_uppercase(b: &mut Bencher) {
71     b.iter(|| (128..=255).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
72 }
73
74 #[bench]
75 fn bench_non_ascii_char_to_lowercase(b: &mut Bencher) {
76     b.iter(|| (128..=255).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
77 }