Skip to content

Commit a913fc6

Browse files
committed
Add benchmark for String::shrink_to_fit()
This uses `Vec::shrink_to_fit()` internally so it's really benchmarking that.
1 parent cda3490 commit a913fc6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcollections/string.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,4 +1408,20 @@ mod tests {
14081408
let _ = String::from_utf8_lossy(s.as_slice());
14091409
});
14101410
}
1411+
1412+
#[bench]
1413+
fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
1414+
let s = "Hello there, the quick brown fox jumped over the lazy dog! \
1415+
Lorem ipsum dolor sit amet, consectetur. ";
1416+
// ensure our operation produces an exact-size string before we benchmark it
1417+
let mut r = String::with_capacity(s.len());
1418+
r.push_str(s);
1419+
assert_eq!(r.len(), r.capacity());
1420+
b.iter(|| {
1421+
let mut r = String::with_capacity(s.len());
1422+
r.push_str(s);
1423+
r.shrink_to_fit();
1424+
r
1425+
});
1426+
}
14111427
}

0 commit comments

Comments
 (0)