Add benchmark for String::shrink_to_fit()

This uses `Vec::shrink_to_fit()` internally so it's really benchmarking
that.
This commit is contained in:
Kevin Ballard 2015-01-19 10:21:58 -08:00
parent cda3490f8f
commit a913fc64d2

View file

@ -1408,4 +1408,20 @@ mod tests {
let _ = String::from_utf8_lossy(s.as_slice());
});
}
#[bench]
fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
let s = "Hello there, the quick brown fox jumped over the lazy dog! \
Lorem ipsum dolor sit amet, consectetur. ";
// ensure our operation produces an exact-size string before we benchmark it
let mut r = String::with_capacity(s.len());
r.push_str(s);
assert_eq!(r.len(), r.capacity());
b.iter(|| {
let mut r = String::with_capacity(s.len());
r.push_str(s);
r.shrink_to_fit();
r
});
}
}