2 use test::{black_box, Bencher};
5 fn bench_with_capacity(b: &mut Bencher) {
6 b.iter(|| String::with_capacity(100));
10 fn bench_push_str(b: &mut Bencher) {
11 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
13 let mut r = String::new();
18 const REPETITIONS: u64 = 10_000;
21 fn bench_push_str_one_byte(b: &mut Bencher) {
22 b.bytes = REPETITIONS;
24 let mut r = String::new();
25 for _ in 0..REPETITIONS {
32 fn bench_push_char_one_byte(b: &mut Bencher) {
33 b.bytes = REPETITIONS;
35 let mut r = String::new();
36 for _ in 0..REPETITIONS {
43 fn bench_push_char_two_bytes(b: &mut Bencher) {
44 b.bytes = REPETITIONS * 2;
46 let mut r = String::new();
47 for _ in 0..REPETITIONS {
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. ";
58 assert_eq!(100, s.len());
60 let _ = String::from_utf8_lossy(s);
65 fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
66 let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
67 assert_eq!(100, s.len());
69 let _ = String::from_utf8_lossy(s);
74 fn from_utf8_lossy_invalid(b: &mut Bencher) {
75 let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
77 let _ = String::from_utf8_lossy(s);
82 fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
83 let s = repeat(0xf5).take(100).collect::<Vec<_>>();
85 let _ = String::from_utf8_lossy(&s);
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());
96 assert_eq!(r.len(), r.capacity());
98 let mut r = String::with_capacity(s.len());
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))
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))
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())
127 fn bench_insert_char_short(b: &mut Bencher) {
128 let s = "Hello, World!";
130 let mut x = String::from(s);
131 black_box(&mut x).insert(6, black_box(' '));
137 fn bench_insert_char_long(b: &mut Bencher) {
138 let s = "Hello, World!";
140 let mut x = String::from(s);
141 black_box(&mut x).insert(6, black_box('❤'));
147 fn bench_insert_str_short(b: &mut Bencher) {
148 let s = "Hello, World!";
150 let mut x = String::from(s);
151 black_box(&mut x).insert_str(6, black_box(" "));
157 fn bench_insert_str_long(b: &mut Bencher) {
158 let s = "Hello, World!";
160 let mut x = String::from(s);
161 black_box(&mut x).insert_str(6, black_box(" rustic "));