
Understand it
A benchmark measures a specific artifact and workload. Cargo profile names have defaults that can be changed; there is no universal debug-to-release speed ratio. Match production settings when answering a production question, and separate compilation, initialization and steady-state execution.
Start with a user-visible performance question and a baseline. Is the problem p95 request latency, startup time, battery use, memory footprint, or throughput? These metrics can move in different directions. Match the compiled artifact to the question: production settings for production behaviour, development settings for a development-loop problem. Keep input sizes representative, warm caches deliberately, repeat measurements, and record variance. A profiler points to expensive stacks or allocations; it does not automatically tell you which change is safe or worthwhile.
Suppose a hot path clones a vector on every request. Profile allocation count and bytes, then change the API to borrow the slice if ownership is unnecessary. Verify the result with a correctness test and rerun the same workload. If the clone was actually off the hot path, the optimization may not matter. Vec::with_capacity helps when the final size is known, but over-reserving can waste memory. Bounds-check removal through unsafe should be a late, measured step with a soundness proof, not a reflex.
In an interview, describe the experiment you would run, not just a list of tools. State the control, treatment, sample size, and how you would detect regressions in another metric. Discuss tail latency and realistic load when relevant. A simple algorithmic improvement can beat a clever micro-optimization. The strongest answer ends with a measured conclusion and the tradeoff you accepted.
If a profiler points to a lock, do not immediately replace it with atomics. Check contention, critical-section duration, and whether a redesign can avoid shared mutation. A change that reduces average latency but worsens p99 under load may be unacceptable. Report the metric the user actually experiences.
Interview follow-up: How would you separate cold build, warm build, initialization and steady-state execution? Record variance and verify output correctness.
Read the code
// Allocates a String on every iteration.
for row in rows {
out.push(format!("{}-{}", row.a, row.b));
}The allocation per iteration is a hypothesis to profile, not yet a finding.
Practice starter
cargo build
# Then run a timing comparison.Try it
Profile a slow function and name the top cost before changing anything.
What success looks like: The change you make is the one the profile pointed at.
Check your understanding
Before claiming production performance from this command, what should be checked?
Reveal the answer
- Every development profile has identical timing to a release profile.
- Verify the actual profile, optimization settings and representative measurement conditions.
- Running cargo bench is the only way to obtain any useful performance evidence.
Cargo defaults differ, but profiles are configurable. Development results can diagnose development behaviour; they do not establish production performance or a fixed speed ratio.
Go deeper
Common misconception: Running cargo bench is the only way to obtain any useful performance evidence.
cargo bench is one tool. Profilers, production traces and timed release runs are also evidence, if the artifact and workload match the question.
- Identify the contract: Measure first: the bottleneck is almost never where it feels like it is.
- Trace the concrete example: The allocation per iteration is a hypothesis to profile, not yet a finding.
- Test the changed constraint: How would you separate cold build, warm build, initialization and steady-state execution? Record variance and verify output correctness.
Transfer challenge: How would you separate cold build, warm build, initialization and steady-state execution? Record variance and verify output correctness.
Connections
- API Design and Semver: Making misuse impossible to express
- FFI and ABI: Ownership across the C boundary
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Profiling First (courseId "interview", lessonId "q-perf-profiling") Link: https://crabrix.com/learn/interview/q-perf-profiling/ Goal: Profile, change one thing, measure again. Guessing is not optimising. Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.