Why is your program slow? Profiling can help you find out!
It's easy to do in Rust.
I personally use two tools: hyperfine and cargo-flamegraph.
The former is a benchmarking tool, and the latter is a profiling tool.
That's all I need to find out how to make my program faster.
First, make sure you build the release version of the program:
cargo build --releaseTo benchmark CLI commands, you can use hyperfine. For example, to benchmark the ff command:
hyperfine 'target/release/ff ..'The output will look like this:
Benchmark #1: target/release/ff ..
Time (mean ± σ): 1.3 ms ± 0.1 ms [User: 1.1 ms, System: 0.2 ms]
Range (min … max): 1.1 ms … 1.6 ms 100 runsTo profile the program, you can use cargo-flamegraph.
# Profile `ff` using the current directory
sudo cargo flamegraph --release -- . The output is an SVG file that you can open in a browser.