Skip to content

Instantly share code, notes, and snippets.

@theferrit32
Created August 10, 2019 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save theferrit32/0b5d04458284b2b9c7a2f87b4481f77b to your computer and use it in GitHub Desktop.
Save theferrit32/0b5d04458284b2b9c7a2f87b4481f77b to your computer and use it in GitHub Desktop.
Compare different grep-like methods
#!/bin/bash
if ! which python3 >/dev/null 2>&1; then
echo 'python3 required'
fi
if [ -z "$1" ]; then
echo "greptest.sh <regex|string>"; exit 1
fi
mytime () {
start=`date +%s.%N`
$@ >/dev/null 2>&1
end=`date +%s.%N`
echo $(calc "$end - $start")
}
calc () {
python3 -c "print($1)"
}
get_time () {
out=$(mytime $@)
printf "%s\n" $out
}
test_cmd() {
limit=$1; total=0.0
for i in $(seq 1 $limit); do
val=$(get_time "${@:2}" "${string}")
total=$(calc "$total + $val")
done
echo $(calc "$total / $limit")
}
string=$1
rounds=100
printf 'grep: %s\n' $(test_cmd $rounds grep -r -E)
printf 'git grep: %s\n' $(test_cmd $rounds git grep -E)
printf 'rg: %s\n' $(test_cmd $rounds rg)
printf 'ack: %s\n' $(test_cmd $rounds ack)
printf 'ag: %s\n' $(test_cmd $rounds ag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment