[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: performance bug of [[ $x ]]
From: |
Pierre Gaston |
Subject: |
Re: performance bug of [[ $x ]] |
Date: |
Mon, 9 Mar 2020 13:03:15 +0100 |
On Mon, Mar 9, 2020 at 1:36 AM Peng Yu <address@hidden> wrote:
> 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.
>
> Sorry, yes I meant that and it looks like I didn't test correctly