Skip to content

Instantly share code, notes, and snippets.

@awreece
Last active December 20, 2015 08:39
Show Gist options
  • Save awreece/6101995 to your computer and use it in GitHub Desktop.
Save awreece/6101995 to your computer and use it in GitHub Desktop.
Fun bash fact of the day: keep your variable names short - you can get a noticeable performance improvement! (don't actually do that, if you care about this kind of performance in bash pick a different language -.-)
Testing bash
It took an average of 8.765s to assign to the variable v
It took an average of 8.884s to assign to the variable vvvvv
It took an average of 8.973s to assign to the variable vvvvvvvvvv
It took an average of 9.040s to assign to the variable vvvvvvvvvvvvvvvvvvvv
#!/usr/bin/env zsh
shell=${1:-bash}
echo "Testing $shell"
zmodload zsh/datetime
for name in v vvvvv vvvvvvvvvv vvvvvvvvvvvvvvvvvvvv; do
time=0
for t in {1..5}; do
start=$EPOCHREALTIME
$shell -c "for ((i = 0; i < 1000000; i++)); do $name='x'; done"
end=$EPOCHREALTIME
((time += end-start))
done
printf "It took an average of %.3fs to assign to the variable %s\n" \
$((time / 5)) $name
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment