Skip to content

Instantly share code, notes, and snippets.

@serious-angel
Last active September 18, 2026 22:39
Show Gist options
  • Select an option

  • Save serious-angel/131d33f045ac91e87cb666ade0c5e686 to your computer and use it in GitHub Desktop.

Select an option

Save serious-angel/131d33f045ac91e87cb666ade0c5e686 to your computer and use it in GitHub Desktop.
$ du -sbh ./input.txt;
# 209M ./input.txt

$ ./benchmark.sh ./input.txt;
Benchmark 1: tr -s \n
  Time (mean ± σ):     852.3 ms ±   9.8 ms    [User: 823.6 ms, System: 28.4 ms]
  Range (min … max):   841.4 ms … 871.8 ms    10 runs
 
Benchmark 2: grep -vx ''
  Time (mean ± σ):     564.1 µs ±  35.2 µs    [User: 306.6 µs, System: 221.7 µs]
  Range (min … max):   490.5 µs … 1008.2 µs    4622 runs
 
  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
 
Benchmark 3: grep -v '^$'
  Time (mean ± σ):     566.3 µs ±  34.5 µs    [User: 308.2 µs, System: 221.7 µs]
  Range (min … max):   499.9 µs … 949.6 µs    5425 runs
 
  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
 
Benchmark 4: sed '/^$/d'
  Time (mean ± σ):      2.438 s ±  0.008 s    [User: 2.394 s, System: 0.043 s]
  Range (min … max):    2.427 s …  2.455 s    10 runs
 
Benchmark 5: awk NF
  Time (mean ± σ):      4.090 s ±  0.031 s    [User: 4.048 s, System: 0.041 s]
  Range (min … max):    4.062 s …  4.152 s    10 runs
 
Summary
  grep -vx '' ran
    1.00 ± 0.09 times faster than grep -v '^$'
 1510.85 ± 95.89 times faster than tr -s \n
 4321.33 ± 270.15 times faster than sed '/^$/d'
 7250.52 ± 455.97 times faster than awk NF
#!/usr/bin/env bash
declare -r _commands=(
'tr -s \n'
"grep -vx ''"
"grep -v '^\$'"
"sed '/^\$/d'"
'awk NF'
)
_Main()
{
declare __filepath="$1";
shift || return 2;
hyperfine -N --warmup 3 \
--input "$__filepath" \
"${_commands[@]}";
}
_Main "$@";
#!/usr/bin/env php
$count = 30000000;
$f = fopen('./input.txt',"wb");
for ($i=0; $i < $count; $i++) {
$line = mt_rand(1, 10) > 3 ? "some line\n" : "\n";
fwrite($f, $line);
}
fclose($f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment