On 8/12/18 3:16 AM, Bize Ma wrote: > Try this script: > > #!/bin/bash > TIMEFORMAT='%R' > > n=1000 > m=20000 > > f1 () { :; } > > f2 () { i=0; time while [ "$((i+=1))" -lt "$n" ]; do : ; done > i=0; time while [ "$((i+=1))" -lt "$n" ]; do f1 ; done > } > > test1() { set -- $(seq $m) > f2 "" > f2 "$@" > } > test1 > > To get: > > 0.019 > 0.028 > 0.019 > 19.204 > > Which is a thousand times slower.
If you build a profiling version of bash, you'll find that about 75% of that time is spent copying the list of arguments around, since you have to save and restore it each time you call f1. Looking at making that more efficient has been a low-level task for a while now. > Bash 5 is even worse, try: Bash-5.0-alpha is not a released version, so it uses the debugging malloc. If you profile that, you'll find that about 99% of the time is spent marking allocations as active and free in the table the bash malloc uses to keep track of active memory. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU c...@case.edu http://tiswww.cwru.edu/~chet/