Hyperfine: How to Benchmark Compilation

Updated

Learn how to benchmark compilation times using hyperfine

Hyperfine: How to Benchmark Compilation

Before we start, make sure you have hyperfine installed on your computer and your code ready in a folder. If you don't have it yet, run this command:

brew install hyperfine

First, open your terminal and navigate to the folder that has your code. Build tools skip work when the program already exists, so you need to delete the old build before every run. To benchmark a clean build, run this command:

hyperfine --prepare 'rm -f output' 'gcc main.c -o output'

The --prepare option runs a command before each timing run, and that time is not counted. Here it removes output, so only the compile is timed. Just swap in your own compile command and file names.

If your build is slow, limit the number of runs with --runs 5 and add --warmup 1 to warm up the file cache:

hyperfine --warmup 1 --runs 5 --prepare 'rm -f output' 'gcc main.c -o output'
undefined

One more tip: the same idea works with any build tool. Just use make clean or cargo clean as the prepare command, and your normal build command as the one to time.