Skip to content

Instantly share code, notes, and snippets.

@mafredri
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafredri/25aa77de809e86d24a63 to your computer and use it in GitHub Desktop.
Save mafredri/25aa77de809e86d24a63 to your computer and use it in GitHub Desktop.
ZSH Performance: variable vs captured subshell output
#!/usr/bin/env zsh
test1() {
test_var=$1
}
test2() {
print -n $1
}
test4() {
eval "$1=\$2"
}
zmodload zsh/datetime
typeset -g test_var test_var4
float +E time1 time2 time3 time4
time1=$EPOCHREALTIME
for i in {1..9999}; do
test1 $i
print $test_var > /dev/null
done
(( time1 = EPOCHREALTIME - time1 ))
time2=$EPOCHREALTIME
for i in {1..9999}; do
print $(test2 $i) > /dev/null
done
(( time2 = EPOCHREALTIME - time2 ))
time3=$EPOCHREALTIME
print "$(
for i in {1..9999}; do
test2 $i
done
)" > /dev/null
(( time3 = EPOCHREALTIME - time3 ))
time4=$EPOCHREALTIME
for i in {1..9999}; do
test4 test_var4 $i
print $test_var4 > /dev/null
done
(( time4 = EPOCHREALTIME - time4 ))
print "variable: $time1"
print "subshell: $time2"
print "all-in-one-subshell: $time3"
print "eval-var: $time4"
# Results:
# variable: 0.22123885154724121
# subshell: 9.453078031539917
# all-in-one-subshell: 0.13457703590393066
# eval-var: 0.2532649040222168
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment