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:
parent
cda3490f8f
commit
a913fc64d2
1 changed files with 16 additions and 0 deletions
|
|
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue