liveBook · Manning

Benchmark

As explained in Idiomatic Rust

A benchmark is a repeatable performance measurement of a program operation under specified conditions.

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

How Idiomatic Rust covers it

How deep it goes
Shows benchmark functions and measured results for three operations across Vec, HashSet, and LinkedList.
Examples
Times appending a million elements, finding random values, and removing elements from collections.
What you can do after
Evaluate collection choices by running Rust benchmarks with cargo bench and comparing workload timings.
How it connects
Connects benchmarking with Vec, HashSet, LinkedList, and operation complexity.

Also explained in

Compare how 23 books explain benchmark →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.