Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active January 17, 2021 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/9780744 to your computer and use it in GitHub Desktop.
Save smoser/9780744 to your computer and use it in GitHub Desktop.
shell benchmarks: compare grep and pipe versus awk or sed only
LANG=C
3156 out.full
76 out.100
50 out.50
20 out.20
3302 total
awk: /usr/bin/gawk
sed: /bin/sed
grep: /bin/grep
----
100 awkonly out.full real 0m1.234s user 0m0.838s sys 0m0.390s
100 sedonly out.full real 0m1.130s user 0m0.763s sys 0m0.348s
100 grep_sed out.full real 0m0.718s user 0m0.294s sys 0m0.600s
100 grep_sed_stdin out.full real 0m0.720s user 0m0.182s sys 0m0.599s
100 grep_awk out.full real 0m1.134s user 0m0.728s sys 0m0.525s
100 awkonly out.100 real 0m0.591s user 0m0.252s sys 0m0.336s
100 sedonly out.100 real 0m0.505s user 0m0.139s sys 0m0.351s
100 grep_sed out.100 real 0m0.688s user 0m0.172s sys 0m0.638s
100 grep_sed_stdin out.100 real 0m0.667s user 0m0.126s sys 0m0.695s
100 grep_awk out.100 real 0m0.733s user 0m0.299s sys 0m0.558s
100 awkonly out.50 real 0m0.576s user 0m0.200s sys 0m0.357s
100 sedonly out.50 real 0m0.516s user 0m0.163s sys 0m0.330s
100 grep_sed out.50 real 0m0.645s user 0m0.139s sys 0m0.622s
100 grep_sed_stdin out.50 real 0m0.639s user 0m0.086s sys 0m0.663s
100 grep_awk out.50 real 0m0.694s user 0m0.227s sys 0m0.577s
100 awkonly out.20 real 0m0.581s user 0m0.222s sys 0m0.352s
100 sedonly out.20 real 0m0.508s user 0m0.143s sys 0m0.352s
100 grep_sed out.20 real 0m0.708s user 0m0.171s sys 0m0.642s
100 grep_sed_stdin out.20 real 0m0.611s user 0m0.148s sys 0m0.619s
100 grep_awk out.20 real 0m0.677s user 0m0.167s sys 0m0.594s
100 python_exit real 0m4.633s user 0m3.229s sys 0m1.269s
100 python3_exit real 0m10.350s user 0m8.285s sys 0m1.690s
100 python_import_sys real 0m4.570s user 0m3.240s sys 0m1.300s
100 python3_import_sys real 0m10.111s user 0m8.295s sys 0m1.595s
100 perl_exit real 0m0.719s user 0m0.285s sys 0m0.429s
#!/bin/bash
##
## pipe-and-grep versus no pipe
##
## try to compare the time for
## grep file | awk
## versus
## awk
##
## Ie, compare the overhead of the '|' with the better
## filter performance in grep.
awkonly() {
sh -fc '
file="$0"
shift;
for x in "$@"; do
awk '\''$3 == "DMI:" { sub(/.*DMI:/,"", $0); print($0); }'\'' "$file";
done
' "$@"
}
grep_sed() {
sh -fc '
file="$0"
shift;
for x in "$@"; do
grep DMI: "$file" | sed "s,.*DMI:,,"
done
' "$@"
}
grep_sed_stdin() {
sh -fc '
file="$0"
shift;
for x in "$@"; do
grep DMI: < "$file" | sed "s,.*DMI:,,"
done
' "$@"
}
grep_awk() {
sh -fc '
file="$0"
shift;
for x in "$@"; do
grep DMI: "$file" |
awk '\''$3 == "DMI:" { sub(/.*DMI:/,"", $0); print($0); }'\'' "$file";
done
' "$@"
}
sedonly() {
sh -fc '
file="$0"
shift;
for x in "$@"; do
sed -n "/DMI:/s/.*DMI://p" "$file"
done
' "$@"
}
python_exit() {
sh -fc '
for x in "$@"; do
python -c "exit(0);"
done
' "$@"
}
python_import_sys() {
sh -fc '
for x in "$@"; do
python -c "import sys; sys.exit(0);"
done
' "$@"
}
perl_exit() {
sh -fc '
for x in "$@"; do
perl -e "exit(0);";
done
' "$@"
}
python3_exit() {
sh -fc '
for x in "$@"; do
python3 -c "exit(0);"
done
' "$@"
}
dotime() {
local x=0 count=$1 aargs=""
shift
while [ "$x" -le "$count" ] && x=$(($x+1)); do aargs="$aargs ."; done
printf "%-40s" "$count $*" 1>&2
TIMEFORMAT='real %3lR user %3lU sys %3lS'
time "$@" $aargs
echo
}
echo "LANG=$LANG"
dmesg > out.full
dmesg | grep --before-context=49 --after-context=50 "DMI:" > out.100
dmesg | grep --before-context=24 --after-context=25 "DMI:" > out.50
dmesg | grep --before-context=9 --after-context=10 "DMI:" > out.20
wc -l out.full out.100 out.50 out.20
echo "awk: $(readlink -f `which awk`)"
echo "sed: $(readlink -f `which sed`)"
echo "grep: $(readlink -f `which grep`)"
echo ----
ops="awkonly sedonly grep_sed grep_sed_stdin grep_awk"
for f in out.full out.100 out.50 out.20; do
for op in $ops; do
dotime 100 $op "$f" >/dev/null
done
done
## just for fun, show other utils just loading
dotime 100 python_exit >/dev/null
dotime 100 python3_exit >/dev/null
dotime 100 python_import_sys >/dev/null
dotime 100 perl_exit >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment