How to Use the hyperfine Command in Linux to Benchmark CLI Tools and Scripts

Timing command-line utilities, shell scripts, and build pipelines in Linux has traditionally relied on the basic shell built-in time. However, simple timing commands are vulnerable to cache cold-starts, thermal throttling, and background system interference, providing little statistical insight into performance variance. Hyperfine is a modern, Rust-built command-line benchmarking tool designed to measure program execution speed with scientific rigor, statistical metrics, and comparative side-by-side analysis.

Key Features of Hyperfine

Hyperfine eliminates guesswork when comparing alternative CLI tools (such as grep vs ripgrep, or find vs fd):

  • Statistical Rigor: Executes multiple benchmark iterations, reporting mean execution time, standard deviation, minimum, and maximum durations.
  • Warm-Up Iterations: Allows you to execute throwaway warm-up runs to populate disk caches and file buffers before recording live metrics.
  • Outlier Detection: Automatically identifies anomalies caused by background process spikes and prompts you to review inconsistent iterations.
  • Side-by-Side Comparison: Compares two or more distinct commands simultaneously, computing relative speedup factors (e.g., “Command A ran 4.2x faster than Command B”).
  • Export Formats: Exports benchmark tables directly to Markdown, CSV, JSON, and AsciiDoc for technical documentation and CI/CD pipelines.

Installing Hyperfine on Linux

Hyperfine is packaged across official software channels for major distributions:

Ubuntu (22.04 LTS and Newer) & Debian

sudo apt update
sudo apt install hyperfine

Arch Linux and Manjaro

sudo pacman -S hyperfine

Fedora, RHEL, and Rocky Linux

sudo dnf install hyperfine

Cargo (Cross-Distribution)

If you have Rust’s package manager installed:

cargo install hyperfine

Running a Basic Benchmark

To measure the execution time of a single shell command or custom binary, pass the command string inside quotation marks to hyperfine:

hyperfine 'gzip -k large_log_file.log'

Hyperfine automatically determines how many iterations are necessary to achieve a reliable sample size (typically between 10 and 100 runs), displaying an animated progress bar and outputting a formatted statistical breakdown:

Benchmark 1: gzip -k large_log_file.log
  Time (mean ± σ):     845.2 ms ±  14.6 ms    [User: 812.0 ms, System: 31.4 ms]
  Range (min … max):   822.4 ms … 871.9 ms    10 runs

Comparing Two Commands with Relative Speedup

The most compelling capability of Hyperfine is comparing competing command implementations. For instance, comparing standard find against modern fd when searching a deep directory tree:

hyperfine 'find . -name "*.py"' 'fd -e py'

Hyperfine benchmarks both commands sequentially under identical system loads and outputs an intuitive comparative summary:

Summary
  'fd -e py' ran
    6.14 ± 0.32 times faster than 'find . -name "*.py"'

Using Warm-Up Runs to Eliminate Disk Cache Bias

When benchmarking disk-heavy commands, the first iteration often takes substantially longer due to cold disk reads from physical storage, whereas subsequent runs hit Linux page cache in RAM. To normalize this, use the -w (warm-up) flag:

hyperfine --warmup 3 'grep -r "TODO" /var/www/project'

Hyperfine will run the command three times to pre-populate file system caches before initiating formal metric recordings.

Parameterised Benchmarking Across Variable Inputs

If you want to test how a script scales across different thread counts, compression levels, or file sizes, use the parameterised -P switch:

hyperfine -P threads 1 8 'xz -T {threads} -k archive.tar'

Hyperfine systematically substitutes {threads} with values 1 through 8, graphing execution times across the sweep to help you identify optimal concurrency settings.

Exporting Benchmark Results to Markdown or JSON

For engineering reports, pull requests, or continuous integration dashboards, you can pipe formatted tables directly into files:

hyperfine --export-markdown benchmark_results.md 'node server.js' 'bun server.js'

This generates a clean GitHub-flavoured Markdown table complete with command names, mean times, and speedup ratios, turning tedious performance profiling into a reproducible automated workflow.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.