Skip to content

Commit 1668984

Browse files
committed
document how to do good performance work
1 parent 8931d2c commit 1668984

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

docs/src/performance.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Performance Profiling Tutorial
2+
3+
## Effective Benchmarking with Hyperfine
4+
5+
[Hyperfine](https://github.com/sharkdp/hyperfine) is a powerful command-line benchmarking tool that allows you to measure and compare execution times of commands with statistical rigor.
6+
7+
### Benchmarking Best Practices
8+
9+
When evaluating performance improvements, always set up your benchmarks to compare:
10+
11+
1. The GNU implementation as reference
12+
2. The implementation without the change
13+
3. The implementation with your change
14+
15+
This three-way comparison provides clear insights into:
16+
- How your implementation compares to the standard (GNU)
17+
- The actual performance impact of your specific change
18+
19+
### Example Benchmark
20+
21+
First, you will need to build the binary in release mode. Debug builds are significantly slower:
22+
23+
```bash
24+
cargo build --features unix --release
25+
```
26+
27+
```bash
28+
# Three-way comparison benchmark
29+
hyperfine \
30+
--warmup 3 \
31+
"/usr/bin/ls -R ." \
32+
"./target/release/coreutils.prev ls -R ." \
33+
"./target/release/coreutils ls -R ."
34+
35+
# can be simplified with:
36+
hyperfine \
37+
--warmup 3 \
38+
-L ls /usr/bin/ls,"./target/release/coreutils.prev ls","./target/release/coreutils ls" \
39+
"{ls} -R ."
40+
```
41+
42+
```
43+
# to improve the reproducibility of the results:
44+
taskset -c 0
45+
```
46+
47+
### Interpreting Results
48+
49+
Hyperfine provides summary statistics including:
50+
- Mean execution time
51+
- Standard deviation
52+
- Min/max times
53+
- Relative performance comparison
54+
55+
Look for consistent patterns rather than focusing on individual runs, and be aware of system noise that might affect results.
56+
57+
## Using Samply for Profiling
58+
59+
[Samply](https://github.com/mstange/samply) is a sampling profiler that helps you identify performance bottlenecks in your code.
60+
61+
### Basic Profiling
62+
63+
```bash
64+
# Generate a flame graph for your application
65+
samply record ./target/debug/coreutils ls -R
66+
67+
# Profile with higher sampling frequency
68+
samply record --rate 1000 ./target/debug/coreutils seq 1 1000
69+
```
70+
71+
## Workflow: Measuring Performance Improvements
72+
73+
1. **Establish baselines**:
74+
```bash
75+
hyperfine --warmup 3 \
76+
"/usr/bin/sort large_file.txt" \
77+
"our-sort-v1 large_file.txt"
78+
```
79+
80+
2. **Identify bottlenecks**:
81+
```bash
82+
samply record ./our-sort-v1 large_file.txt
83+
```
84+
85+
3. **Make targeted improvements** based on profiling data
86+
87+
4. **Verify improvements**:
88+
```bash
89+
hyperfine --warmup 3 \
90+
"/usr/bin/sort large_file.txt" \
91+
"our-sort-v1 large_file.txt" \
92+
"our-sort-v2 large_file.txt"
93+
```
94+
95+
5. **Document performance changes** with concrete numbers
96+
```bash
97+
hyperfine --export-markdown file.md [...]
98+
```

0 commit comments

Comments
 (0)