]> git.lizzy.rs Git - rust.git/blob - library/alloc/benches/string.rs
Rollup merge of #104487 - klensy:ntapi, r=Mark-Simulacrum
[rust.git] / library / alloc / benches / string.rs
1 use std::iter::repeat;
2 use test::{black_box, Bencher};
3
4 #[bench]
5 fn bench_with_capacity(b: &mut Bencher) {
6     b.iter(|| String::with_capacity(100));
7 }
8
9 #[bench]
10 fn bench_push_str(b: &mut Bencher) {
11     let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
12     b.iter(|| {
13         let mut r = String::new();
14         r.push_str(s);
15     });
16 }
17
18 const REPETITIONS: u64 = 10_000;
19
20 #[bench]
21 fn bench_push_str_one_byte(b: &mut Bencher) {
22     b.bytes = REPETITIONS;
23     b.iter(|| {
24         let mut r = String::new();
25         for _ in 0..REPETITIONS {
26             r.push_str("a")
27         }
28     });
29 }
30
31 #[bench]
32 fn bench_push_char_one_byte(b: &mut Bencher) {
33     b.bytes = REPETITIONS;
34     b.iter(|| {
35         let mut r = String::new();
36         for _ in 0..REPETITIONS {
37             r.push('a')
38         }
39     });
40 }
41
42 #[bench]
43 fn bench_push_char_two_bytes(b: &mut Bencher) {
44     b.bytes = REPETITIONS * 2;
45     b.iter(|| {
46         let mut r = String::new();
47         for _ in 0..REPETITIONS {
48             r.push('â')
49         }
50     });
51 }
52
53 #[bench]
54 fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
55     let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
56               Lorem ipsum dolor sit amet, consectetur. ";
57
58     assert_eq!(100, s.len());
59     b.iter(|| {
60         let _ = String::from_utf8_lossy(s);
61     });
62 }
63
64 #[bench]
65 fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
66     let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
67     assert_eq!(100, s.len());
68     b.iter(|| {
69         let _ = String::from_utf8_lossy(s);
70     });
71 }
72
73 #[bench]
74 fn from_utf8_lossy_invalid(b: &mut Bencher) {
75     let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
76     b.iter(|| {
77         let _ = String::from_utf8_lossy(s);
78     });
79 }
80
81 #[bench]
82 fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
83     let s = repeat(0xf5).take(100).collect::<Vec<_>>();
84     b.iter(|| {
85         let _ = String::from_utf8_lossy(&s);
86     });
87 }
88
89 #[bench]
90 fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
91     let s = "Hello there, the quick brown fox jumped over the lazy dog! \
92              Lorem ipsum dolor sit amet, consectetur. ";
93     // ensure our operation produces an exact-size string before we benchmark it
94     let mut r = String::with_capacity(s.len());
95     r.push_str(s);
96     assert_eq!(r.len(), r.capacity());
97     b.iter(|| {
98         let mut r = String::with_capacity(s.len());
99         r.push_str(s);
100         r.shrink_to_fit();
101         r
102     });
103 }
104
105 #[bench]
106 fn bench_from_str(b: &mut Bencher) {
107     let s = "Hello there, the quick brown fox jumped over the lazy dog! \
108              Lorem ipsum dolor sit amet, consectetur. ";
109     b.iter(|| String::from(s))
110 }
111
112 #[bench]
113 fn bench_from(b: &mut Bencher) {
114     let s = "Hello there, the quick brown fox jumped over the lazy dog! \
115              Lorem ipsum dolor sit amet, consectetur. ";
116     b.iter(|| String::from(s))
117 }
118
119 #[bench]
120 fn bench_to_string(b: &mut Bencher) {
121     let s = "Hello there, the quick brown fox jumped over the lazy dog! \
122              Lorem ipsum dolor sit amet, consectetur. ";
123     b.iter(|| s.to_string())
124 }
125
126 #[bench]
127 fn bench_insert_char_short(b: &mut Bencher) {
128     let s = "Hello, World!";
129     b.iter(|| {
130         let mut x = String::from(s);
131         black_box(&mut x).insert(6, black_box(' '));
132         x
133     })
134 }
135
136 #[bench]
137 fn bench_insert_char_long(b: &mut Bencher) {
138     let s = "Hello, World!";
139     b.iter(|| {
140         let mut x = String::from(s);
141         black_box(&mut x).insert(6, black_box('❤'));
142         x
143     })
144 }
145
146 #[bench]
147 fn bench_insert_str_short(b: &mut Bencher) {
148     let s = "Hello, World!";
149     b.iter(|| {
150         let mut x = String::from(s);
151         black_box(&mut x).insert_str(6, black_box(" "));
152         x
153     })
154 }
155
156 #[bench]
157 fn bench_insert_str_long(b: &mut Bencher) {
158     let s = "Hello, World!";
159     b.iter(|| {
160         let mut x = String::from(s);
161         black_box(&mut x).insert_str(6, black_box(" rustic "));
162         x
163     })
164 }