<!--
title: Benchmark before optimizing code
domain: runtimes
topic: Performance Optimization
language: Rust
source: rust-lang/rust
updated: 2025-07-23
url: https://awesomereviewers.com/reviewers/rust-benchmark-before-optimizing-code/
-->

Always validate performance optimizations with benchmarks before implementation. Ensure measurements account for:
1. Platform-specific characteristics (32-bit vs 64-bit)
2. Realistic usage patterns
3. Appropriate metrics (wall time vs CPU time)

Example:
```rust
#[bench]
fn write_64bit_hex(bh: &mut Bencher) {
    // Test common cases first
    bh.iter(|| {
        write!(black_box(discard), "{:x}", black_box(0_u64)).unwrap();
        write!(black_box(discard), "{:x}", black_box(10000_i64)).unwrap();
        // Test edge cases
        write!(black_box(discard), "{:x}", black_box(-10000_i64)).unwrap();
    });
}
```

Avoid premature optimization based on theoretical gains. For instance, reusing buffers might seem beneficial but benchmarks often show stack allocation is equally efficient:
```rust
// Prefer simple, clear code unless benchmarks show meaningful gains
let mut buf = NumBuffer::new();  // Creates temporary buffer on stack
format!("{}", number)            // Let compiler optimize common case
```
