Skip to content

Commit

Permalink
Add benchmark for String::shrink_to_fit()
Browse files Browse the repository at this point in the history
This uses `Vec::shrink_to_fit()` internally so it's really benchmarking
that.
  • Loading branch information
lilyball committed Jan 19, 2015
1 parent cda3490 commit a913fc6
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -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
});
}
}

0 comments on commit a913fc6

Please sign in to comment.