Skip to content

Instantly share code, notes, and snippets.

@emdash
Created May 26, 2022 02:40
Show Gist options
  • Save emdash/8481431c67603e4cdc091a403abd34cd to your computer and use it in GitHub Desktop.
Save emdash/8481431c67603e4cdc091a403abd34cd to your computer and use it in GitHub Desktop.
Shorter Benchmark
set -eo pipefail
function just_count {
local -i i=0
local -i n="$1"
while test "${i}" -lt "${n}"
do
i="$(( i + 1 ))"
done
}
function count_and_print {
local -i i=0
local -i n="$1"
while test "${i}" -lt "${n}"
do
i="$(( i + 1 ))"
echo "${i}"
done
}
function count_and_subst {
local -i i=0
local -i n="$1"
local id="$(uuid)"
while test "${i}" -lt "${n}"
do
i="$(( i + 1 ))"
echo "$(echo "${id}")"
done
}
function just_read {
while IFS='' read -r line
do
:
done
}
function read_and_print {
while IFS='' read -r line
do
echo "${line}"
done
}
function benchmark {
local -r func="$1"
time "$@"
}
declare -r size="$1"
echo "Benchmark with size ${size}"
echo "Just Count"
time just_count "${size}"
echo "count_and_print"
time count_and_print "${size}" > /dev/null
echo "count_and_print | just_read"
time count_and_print "${size}" | just_read
echo "count_and_print | read_and_print"
time count_and_print "${size}" | read_and_print > /dev/null
echo "count_and_subst"
time count_and_subst "${size}" > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment