Profiling CPU and allocations
Quick answer
Profiling identifies where a program spends resources. CPU sampling estimates where execution time accumulates; allocation profiling examines heap activity. Neither is identical to wall-clock latency measurement.
Find the bottleneck before editing
A flame graph summarizes sampled stacks; wide frames often indicate many samples, not necessarily a single slow call. Allocation counts and retained bytes answer different questions. On supported Linux systems perf can sample CPU activity, subject to permissions and available symbols. Build an optimized binary with debug information if needed for useful stacks; record profiling overhead. The first example measures logical work only, not allocator calls.
fn main() { let labels = ["ab", "rust", "x"]; let bytes: usize = labels.iter().map(|s| s.len()).sum(); let owned: Vec<String> = labels.iter().map(|s| s.to_string()).collect(); println!("{} {}", owned.len(), bytes);}Expected stdout (not executed):
3 7Seven payload bytes do not imply seven heap bytes or exactly three allocator calls.
cargo +1.85.0 build --releaseperf stat -- ./target/release/demoManual terminal step; not executed. Expected behavior (environment dependent):
CPU and timing statistics or a permission/tool error; values depend on workload and hardware.Use an already installed perf and a project that performs a repeatable workload.
Common mistake
Do not optimize the function with the most calls without checking its actual cost.
Try it yourself
Compare the logical copied bytes before and after borrowing labels; choose a profiler to verify heap behavior.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Profiling CPU and allocations — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A flame graph frame is wide. What is a reasonable interpretation?
Profiling CPU and allocations — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let labels = ["ab", "rust", "x"]; let bytes: usize = labels.iter().map(|s| s.len()).sum(); let owned: Vec<String> = labels.iter().map(|s| s.to_string()).collect(); println!("{} {}", owned.len(), bytes); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Profiling CPU and allocations — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash cargo +1.85.0 build --release perf stat -- ./target/release/demo ``` Which outcome or interpretation best matches this example?