]> git.proxmox.com Git - rustc.git/blob - src/libcollections/benches/string.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / libcollections / benches / string.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::iter::repeat;
12 use test::Bencher;
13
14 #[bench]
15 fn bench_with_capacity(b: &mut Bencher) {
16 b.iter(|| String::with_capacity(100));
17 }
18
19 #[bench]
20 fn bench_push_str(b: &mut Bencher) {
21 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
22 b.iter(|| {
23 let mut r = String::new();
24 r.push_str(s);
25 });
26 }
27
28 const REPETITIONS: u64 = 10_000;
29
30 #[bench]
31 fn bench_push_str_one_byte(b: &mut Bencher) {
32 b.bytes = REPETITIONS;
33 b.iter(|| {
34 let mut r = String::new();
35 for _ in 0..REPETITIONS {
36 r.push_str("a")
37 }
38 });
39 }
40
41 #[bench]
42 fn bench_push_char_one_byte(b: &mut Bencher) {
43 b.bytes = REPETITIONS;
44 b.iter(|| {
45 let mut r = String::new();
46 for _ in 0..REPETITIONS {
47 r.push('a')
48 }
49 });
50 }
51
52 #[bench]
53 fn bench_push_char_two_bytes(b: &mut Bencher) {
54 b.bytes = REPETITIONS * 2;
55 b.iter(|| {
56 let mut r = String::new();
57 for _ in 0..REPETITIONS {
58 r.push('â')
59 }
60 });
61 }
62
63 #[bench]
64 fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
65 let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
66 Lorem ipsum dolor sit amet, consectetur. ";
67
68 assert_eq!(100, s.len());
69 b.iter(|| {
70 let _ = String::from_utf8_lossy(s);
71 });
72 }
73
74 #[bench]
75 fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
76 let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
77 assert_eq!(100, s.len());
78 b.iter(|| {
79 let _ = String::from_utf8_lossy(s);
80 });
81 }
82
83 #[bench]
84 fn from_utf8_lossy_invalid(b: &mut Bencher) {
85 let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
86 b.iter(|| {
87 let _ = String::from_utf8_lossy(s);
88 });
89 }
90
91 #[bench]
92 fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
93 let s = repeat(0xf5).take(100).collect::<Vec<_>>();
94 b.iter(|| {
95 let _ = String::from_utf8_lossy(&s);
96 });
97 }
98
99 #[bench]
100 fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
101 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
102 Lorem ipsum dolor sit amet, consectetur. ";
103 // ensure our operation produces an exact-size string before we benchmark it
104 let mut r = String::with_capacity(s.len());
105 r.push_str(s);
106 assert_eq!(r.len(), r.capacity());
107 b.iter(|| {
108 let mut r = String::with_capacity(s.len());
109 r.push_str(s);
110 r.shrink_to_fit();
111 r
112 });
113 }
114
115 #[bench]
116 fn bench_from_str(b: &mut Bencher) {
117 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
118 Lorem ipsum dolor sit amet, consectetur. ";
119 b.iter(|| String::from(s))
120 }
121
122 #[bench]
123 fn bench_from(b: &mut Bencher) {
124 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
125 Lorem ipsum dolor sit amet, consectetur. ";
126 b.iter(|| String::from(s))
127 }
128
129 #[bench]
130 fn bench_to_string(b: &mut Bencher) {
131 let s = "Hello there, the quick brown fox jumped over the lazy dog! \
132 Lorem ipsum dolor sit amet, consectetur. ";
133 b.iter(|| s.to_string())
134 }