A benchmark measures an operation’s performance under repeatable conditions. Comparing collection workloads can reveal that a familiar choice like Vec performs well without being fastest at everything. section 10.4
Collection workloads
Compare appending one million elements, finding 1,000 random values in a million-element collection, and removing 1,000 elements. Vec beats LinkedList in all three; HashSet is faster at finding and removing, but slower at appending. section 10.4
Listing 10.7 Benchmarking Vec, HashSet, and LinkedList for appending
#[bench]
fn vec_append(b: &mut Bencher) {
b.iter(|| {
let mut nums: Vec<i32> = Vec::new();
for n in 0..1_000_000 {
nums.push(n);
}
});
}
#[bench]
fn list_append(b: &mut Bencher) {
b.iter(|| {
let mut nums: LinkedList<i32> = LinkedList::new();
for n in 0..1_000_000 {
nums.push_back(n);
}
});
}
#[bench]
fn set_append(b: &mut Bencher) {
b.iter(|| {
let mut nums: HashSet<i32> = HashSet::new();
for n in 0..1_000_000 {
nums.insert(n);
}
});
}
The Rust example benchmarks appending one million integers to a Vec, LinkedList, and HashSet. section 10.4






















