bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unexpected delay in using arguments.


From: Chet Ramey
Subject: Re: Unexpected delay in using arguments.
Date: Tue, 14 Aug 2018 11:25:04 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

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    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]