Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active January 2, 2019 08:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/dd4deaa3b9b0e2a59198f28e6334ef36 to your computer and use it in GitHub Desktop.
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
#!/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