Timing a command once with time tells you very little, because the second run is usually faster than the first. A benchmark tool repeats the runs and does the arithmetic.
sudo apt install hyperfine
hyperfine 'grep -r TODO src' 'rg TODO src'
Each command is run many times, and you get the mean, the spread and a line saying how much faster one was than the other. Two options make the comparison fairer. Warm up the file cache first:
hyperfine --warmup 3 'grep -r TODO src' 'rg TODO src'
Or prepare a clean state before every single run:
hyperfine --prepare 'rm -rf build' 'make'
To fix the number of runs, use -r 10. For very quick commands, -N runs them without a shell, so the shell’s own startup does not distort the numbers.
I like it for settling small arguments, such as whether a flag really makes a script faster.
Check what the --prepare command deletes before you run it, because it runs again and again. The numbers also only hold for the machine, data and moment you measured. A laptop on battery or a busy server gives noisy results, and hyperfine will warn about outliers. Make sure both commands do the same work, or the winner is just the one that does less.
Further reading: hyperfine on GitHub