help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Is [[ $x = $y ]] faster than [ "$x" = "$y" ]?


From: Andy Chu
Subject: Re: [Help-bash] Is [[ $x = $y ]] faster than [ "$x" = "$y" ]?
Date: Sat, 29 Jul 2017 15:23:13 -0700

One reason that [[ might be faster is that bash parses it once up front,
whereas a [ expression is parsed on each iteration of the loop.

In other words, [[ is part of the language, parsed at the same time as the
while loop, in the "parse.y" file.  But [ is a builtin, which is treated
like an external command -- it gets an argv array on every invocation.

http://www.oilshell.org/blog/2016/10/12.html

As I mention there, I don't think this difference is adequately captured in
'help [['.

Andy



On Sat, Jul 29, 2017 at 2:19 PM, Peng Yu <address@hidden> wrote:

> Hi,
>
> I have the following test case. Can I make the conclusion that [[ $x =
> $y ]] is faster than [ "$x" = "$y" ] in general? Thanks.
>
> $  ./main.sh
>
> real    0m3.769s
> user    0m3.739s
> sys    0m0.005s
>
> real    0m7.890s
> user    0m7.579s
> sys    0m0.010s
>
>
> $ ./main.sh
>
> real    0m3.769s
> user    0m3.739s
> sys    0m0.005s
>
> real    0m7.890s
> user    0m7.579s
> sys    0m0.010s
> $ cat main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> declare -i i
> declare -i n
> n=500000
>
> i=0
> time while ((i<n))
> do
>     [[ $x = $y ]]
>     ((++i))
> done
>
> i=0
> time while ((i<n))
> do
>     [ "$x" = "$y" ]
>     ((++i))
> done
>
> --
> Regards,
> Peng
>
>


reply via email to

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