help-bash
[Top][All Lists]
Advanced

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

Re: Which is faster `[[ -v x[k] ]]` vs `[[ ${x[k]} ]]`?


From: Peng Yu
Subject: Re: Which is faster `[[ -v x[k] ]]` vs `[[ ${x[k]} ]]`?
Date: Wed, 30 Oct 2019 14:15:45 -0500

I don't think you addressed my question. I don't care whether they are
exactly the same as bash syntax.

For my usage case, a key that does not exist or a key with an empty
value is considered functionally the same.

What I don't understand is my the runtime difference depends on how
many times the code runs. This does not make sense to me.

On 10/28/19, Stephane Chazelas <address@hidden> wrote:
> 2019-10-27 18:58:22 -0500, Peng Yu:
> [...]
>> I am confused about which one is faster `[[ -v x[k] ]]` vs `[[ ${x[k]}
>> ]]`. Depending on how long it runs, it seems the runtime comparison
>> results are different. Why? Which one is supposed to be faster?
> [...]
>
> They're not functionaly equivalent.
>
> [[ -v x[k] ]] tests whether the x sparse array has an element
> for key k, [[ ${x[k]} ]] tests whether ${x[k]} expands to a
> non-empty string (for which you need the x sparse array to have
> an element for key k and that element being non-empty).
>
> $ x[12]= k=12
> $ [[ -v x[k] ]] && echo yes
> yes
> $ [[ ${x[k]} ]] || echo no
> no
>
> --
> Stephane
>
>
>
>


-- 
Regards,
Peng



reply via email to

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