diff options
| author | Kevin Ballard <kevin@sb.org> | 2015-01-19 10:21:58 -0800 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2015-01-19 11:10:57 -0800 |
| commit | a913fc64d2f179dfcf7b11895f5b6c7c24d6979c (patch) | |
| tree | 56583305598c284cf5d694d8c0a35db92f7fd6a9 | |
| parent | cda3490f8fb099a1ddd1f253a75c29110042dc01 (diff) | |
| download | rust-a913fc64d2f179dfcf7b11895f5b6c7c24d6979c.tar.gz rust-a913fc64d2f179dfcf7b11895f5b6c7c24d6979c.zip | |
Add benchmark for String::shrink_to_fit()
This uses `Vec::shrink_to_fit()` internally so it's really benchmarking that.
| -rw-r--r-- | src/libcollections/string.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 5d35d8a8679..e15bfdf912b 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -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 + }); + } } |
