]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
11 use std
::iter
::repeat
;
15 fn bench_with_capacity(b
: &mut Bencher
) {
16 b
.iter(|| String
::with_capacity(100));
20 fn bench_push_str(b
: &mut Bencher
) {
21 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
23 let mut r
= String
::new();
28 const REPETITIONS
: u64 = 10_000;
31 fn bench_push_str_one_byte(b
: &mut Bencher
) {
32 b
.bytes
= REPETITIONS
;
34 let mut r
= String
::new();
35 for _
in 0..REPETITIONS
{
42 fn bench_push_char_one_byte(b
: &mut Bencher
) {
43 b
.bytes
= REPETITIONS
;
45 let mut r
= String
::new();
46 for _
in 0..REPETITIONS
{
53 fn bench_push_char_two_bytes(b
: &mut Bencher
) {
54 b
.bytes
= REPETITIONS
* 2;
56 let mut r
= String
::new();
57 for _
in 0..REPETITIONS
{
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. ";
68 assert_eq
!(100, s
.len());
70 let _
= String
::from_utf8_lossy(s
);
75 fn from_utf8_lossy_100_multibyte(b
: &mut Bencher
) {
76 let s
= "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
77 assert_eq
!(100, s
.len());
79 let _
= String
::from_utf8_lossy(s
);
84 fn from_utf8_lossy_invalid(b
: &mut Bencher
) {
85 let s
= b
"Hello\xC0\x80 There\xE6\x83 Goodbye";
87 let _
= String
::from_utf8_lossy(s
);
92 fn from_utf8_lossy_100_invalid(b
: &mut Bencher
) {
93 let s
= repeat(0xf5).take(100).collect
::<Vec
<_
>>();
95 let _
= String
::from_utf8_lossy(&s
);
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());
106 assert_eq
!(r
.len(), r
.capacity());
108 let mut r
= String
::with_capacity(s
.len());
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
))
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
))
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())