Time Command-Line Programs With hyperfine
Benchmark any command with hyperfine 1.19: it runs warm-ups and many repetitions, reports mean, spread and range, and compares commands against each other.

The shell's time builtin gives you one measurement, and one measurement is not a benchmark. The number changes from run to run, the first run is slower than the second because the files are not cached yet, and time tells you nothing about how much to trust the result. hyperfine is a command-line benchmarking tool that fixes these problems by running a command many times, discarding warm-ups, and printing the mean, the standard deviation and the range, plus a relative comparison when you give it more than one command. This post uses hyperfine 1.19 on Linux to time compiled C and C++ programs, compare two implementations, detect unstable results and export numbers for later. It is the quickest way to start measuring native code correctly, and the complete benchmarking guide places it among the other tools.
Install it
hyperfine is a Rust program distributed as a single binary. Install it with your package manager or with Cargo:
cargo install hyperfine
hyperfine --versionhyperfine 1.19.0Distribution packages exist under the name hyperfine for most systems. The project README lists them.
Two programs to compare
The test subjects are two ways to count the words in a large buffer, written in C. The first scans byte by byte with a branch for each character class, and the second uses a lookup table:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
const size_t n = 200u * 1024u * 1024u;
unsigned char *buf = malloc(n);
for (size_t i = 0; i < n; i++)
buf[i] = (i % 7 == 0) ? ' ' : 'a';
size_t words = 0;
int in_word = 0;
for (size_t i = 0; i < n; i++) {
if (isspace(buf[i])) {
in_word = 0;
} else if (!in_word) {
in_word = 1;
words++;
}
}
printf("%zu\n", words);
free(buf);
return 0;
}#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const size_t n = 200u * 1024u * 1024u;
unsigned char *buf = malloc(n);
for (size_t i = 0; i < n; i++)
buf[i] = (i % 7 == 0) ? ' ' : 'a';
unsigned char space[256] = {0};
space[' '] = space['\n'] = space['\t'] = space['\r'] = 1;
size_t words = 0;
unsigned in_word = 0;
for (size_t i = 0; i < n; i++) {
unsigned is_space = space[buf[i]];
words += (~in_word) & (1u ^ is_space);
in_word = 1u ^ is_space;
}
printf("%zu\n", words);
free(buf);
return 0;
}Both print the same number. With a space at every index divisible by 7 and letters elsewhere, each group of six letters is a word, so the count is the number of indices i where i % 7 == 1, which is the number of multiples of 7 in a range shifted by one. For n = 209715200, that is 29959315. Compile both with gcc -O2:
gcc -O2 wc_branch.c -o wc_branch
gcc -O2 wc_table.c -o wc_table29959315Benchmark one command
Pass the command as a single quoted string. hyperfine runs it repeatedly and prints a summary:
hyperfine './wc_branch'Benchmark 1: ./wc_branch
Time (mean ± σ): 412.3 ms ± 6.1 ms [User: 371.8 ms, System: 40.2 ms]
Range (min ... max): 405.7 ms ... 428.9 ms 10 runsThe numbers are illustrative. Read the lines carefully. The mean is the average wall-clock time. The sigma after it is the standard deviation, a measure of spread. A small deviation compared to the mean means a stable measurement. User and System split the CPU time between your code and the kernel, and the system part here is the cost of page-faulting 200 MiB. The range shows the fastest and slowest run, and the run count shows how many hyperfine chose. By default it runs at least 10 times, and more if each run is short, so that the total measurement lasts around three seconds.
Compare two commands
Give hyperfine both commands in one invocation, and it adds a summary that ranks them:
hyperfine --warmup 3 './wc_branch' './wc_table'Benchmark 1: ./wc_branch
Time (mean ± σ): 412.3 ms ± 6.1 ms [User: 371.8 ms, System: 40.2 ms]
Range (min ... max): 405.7 ms ... 428.9 ms 10 runs
Benchmark 2: ./wc_table
Time (mean ± σ): 281.9 ms ± 3.4 ms [User: 241.0 ms, System: 40.5 ms]
Range (min ... max): 277.2 ms ... 288.6 ms 10 runs
Summary
./wc_table ran
1.46 ± 0.03 times faster than ./wc_branchAgain, your numbers will differ. The summary line is the useful part. It divides the means, and propagates the uncertainty into the ratio, so 1.46 ± 0.03 says the table version is faster by about 46 percent, with a margin small enough that you can trust the direction. --warmup 3 runs each command three times before measuring, so the first, page-cache-cold run does not pollute the results.
Fix the run count and handle noise
The automatic run count adapts to how long a command takes. You can override it. Use --runs for an exact number and --min-runs to set a floor:
hyperfine --warmup 3 --runs 30 './wc_table'If the spread is large compared with the mean, hyperfine warns you. It might say that statistical outliers were detected, or that the first run was significantly slower than the rest, which suggests a cold cache and tells you to use --warmup. Take these warnings seriously. A benchmark whose standard deviation is 20 percent of its mean cannot distinguish a 5 percent improvement. The cures are the ones in the mistakes list: more runs, a quieter machine, a pinned core.
Intercept shell and startup overhead
By default hyperfine runs the command through a shell, so the shell's startup time is part of the measurement and is subtracted using a calibration it performs first. For commands that finish in a few milliseconds, that correction is noisy, and hyperfine warns about it. Disable the shell with -N, which runs the command directly:
hyperfine -N --warmup 10 './wc_table'Without a shell, arguments are split on spaces only, so shell features such as pipes, redirections and variable expansion stop working. If you need them, keep the shell and use a longer-running command, or put the pipeline in a script file and benchmark the script.
Save the results
Three export options write the results in formats other tools read:
hyperfine --warmup 3 './wc_branch' './wc_table' \
--export-markdown results.md --export-json results.jsonThe Markdown file holds a comparison table you can paste into a report or a pull request:
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `./wc_branch` | 412.3 ± 6.1 | 405.7 | 428.9 | 1.46 ± 0.03 |
| `./wc_table` | 281.9 ± 3.4 | 277.2 | 288.6 | 1.00 |The JSON file includes every individual run time, which you can load into a script to compute your own statistics. --show-output prints what each run writes to standard output, useful when you want to confirm the program still prints the right answer, and --output can redirect it to a file or to null. By default, hyperfine discards output and does not stop on a nonzero exit status, so a program that crashes immediately can look very fast. Add -i consciously only when you expect a failing exit code. Otherwise leave the default, where a nonzero exit aborts the benchmark and tells you.
What hyperfine does not do
It times whole processes, including startup, dynamic linking and teardown. That is correct for a command-line tool and wrong for measuring a function inside a program, where a framework such as Google Benchmark or a custom harness is the right tool. It also does not control the machine: frequency scaling, other processes and thermal state all affect the numbers, and on a laptop on battery they can swamp the effect you want to see. Pin the machine's state before trusting small differences.
Choose which statistic to read
hyperfine prints the mean and the standard deviation, but those are not always the numbers to compare. The mean is pulled up by a few slow runs, which happen on any machine when an interrupt or a background job lands inside a measurement. The minimum is the least disturbed run and estimates the cost of the program with the least interference. The JSON export carries every run, so you can compute the median yourself:
hyperfine --warmup 3 --runs 30 --export-json out.json './wc_table'
python3 -c "import json,statistics as s; r=json.load(open('out.json'))['results'][0]; print(s.median(r['times']))"0.2813The value is in seconds and illustrative. The median of thirty runs is a robust single figure, and the standard deviation next to it tells you how far to trust it. When two commands differ by less than their standard deviations, the honest conclusion is that you cannot tell them apart with this measurement, and the fix is more runs or a quieter machine, not a bolder claim.
Put it in a script
Because hyperfine is a command-line tool, a performance regression check is a few lines of shell. Run it on the old and new binaries, export JSON, and compare the means in a script. A threshold such as "fail if the new mean is more than 5 percent above the old mean and the difference exceeds twice the standard deviation" avoids both missing real regressions and alarming on noise. Commit the script with the code so that a change in performance is visible in the same place as a change in behavior.


