help-bash
[Top][All Lists]
Advanced

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

Re: performance bug of [[ $x ]]


From: Peng Yu
Subject: Re: performance bug of [[ $x ]]
Date: Sun, 8 Mar 2020 19:36:02 -0500

On 3/8/20, Pierre Gaston <address@hidden> wrote:
> On Sat, Mar 7, 2020 at 11:48 PM Peng Yu <address@hidden> wrote:
>
>> [[ $x ]] just tests whether the variable $x is of length 0 or not. So
>> its performance should not depend on how long the variable is.
>>
>> But the following test case shows that the run time does depend on the
>> length of the variable.
>>
>> Should it be considered as a performance bug of bash?
>>
>
> I guess it could, but I glanced a bit at the code and the test itself
> doesn't seem to depend on the length,
> so I guess the added time is due to the expansion taking place. What bash
> really does is testing the
> length resulting resulting from the expansion, you could have more complex
> case eg [[ $(echo blah)$x$((3+4)) ]]
>
> You would need a special case when you only have a single variable to
> optimize it,  maybe not worth the added
> complexity in the code.

This special case is quite common. It may be worthwhile to add such a
special case.

> as workaround  [[ ${x:1:1} ]] seems to work.

Did you mean [[ ${x:0:1} ]] instead? It still takes a lot of time for
a long variable.

time [[ $x1 ]]

real    0m0.018s
user    0m0.007s
sys     0m0.002s
time [[ $x2 ]]

real    0m0.088s
user    0m0.057s
sys     0m0.014s
time [[ ${x1:0:1} ]]

real    0m0.011s
user    0m0.009s
sys     0m0.000s
time [[ ${x2:0:1} ]]

real    0m0.097s
user    0m0.077s
sys     0m0.005s



-- 
Regards,
Peng



reply via email to

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