Hyperfine: A Shell Benchmarking One-liner
If you were ever curious how long a command takes, do this in your terminal:
time [the thing you wanted to test] , for example:
$ time sleep 1
sleep 1 0.00s user 0.00s system 0% cpu 1.013 total
I already know that and even wrote some casual (but painstakingly hard to read) benchmarking bash scripts, but I wanted to know if there a non-bash ways to reliably test a command or script a few times, find the mean, outliers, etc.
Enter hyperfine:
$ hyperfine --warmup 1 --runs 5 "sleep 1"
Benchmark 1: sleep 1
Time (mean ± σ): 1.016 s ± 0.003 s [User: 0.002 s, System: 0.004 s]
Range (min … max): 1.013 s … 1.020 s 5 runs
And it does basically how it reads: execute the command 1 times as warmup, and runs 5 times,takes the mean and outlier times.
And installation can be done in many ways e.g.
# Using cargo
cargo install hyperfine
# Using mise and lock version to mise config
mise use -g cargo:hyperfine@latest
# Using apt
sudo apt install hyperfine
# Using nix flakes
nix profile add nixpkgs#hyperfine
That’s it! Here’s the hyperlink repo.
P.S. I’ve been loving mise-en-place and has completely replaced asdf as an all-around version manager for me. Try it out too!