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