Last active
January 2, 2019 08:34
-
-
Save RichardBronosky/dd4deaa3b9b0e2a59198f28e6334ef36 to your computer and use it in GitHub Desktop.
A tool for testing the performance of 2 methods of appending strings in bash. Created for https://stackoverflow.com/a/47878161/117471
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
output(){ | |
ptime=$ctime; | |
ctime=$(date +%s.%N); | |
delta=$(bc <<<"$ctime - $ptime"); | |
printf "%2s. %16s chars time: %s delta: %s\n" $n "$(bc <<<"10*(2^$n)")" $ctime $delta; | |
} | |
method1(){ | |
echo 'Method: a="$a$a"' | |
for n in {1..32}; do a="$a$a"; output; done | |
} | |
method2(){ | |
echo 'Method: a+="$a"' | |
for n in {1..32}; do a+="$a"; output; done | |
} | |
ctime=0; a="0123456789"; time method$1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment