help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] How to make sure always run external command instead of


From: Pierre Gaston
Subject: Re: [Help-bash] How to make sure always run external command instead of builtin function?
Date: Tue, 4 Dec 2018 09:29:03 +0200

On Tue, Dec 4, 2018 at 6:57 AM Peng Yu <address@hidden> wrote:

> On Mon, Dec 3, 2018 at 9:19 PM Dennis Williamson
> <address@hidden> wrote:
> >
> >
> >
> > On Mon, Dec 3, 2018, 8:51 PM Peng Yu <address@hidden wrote:
> >>
> >> On Mon, Dec 3, 2018 at 5:22 PM Bob Proulx <address@hidden> wrote:
> >> >
> >> > Eric Blake wrote:
> >> > > Peng Yu wrote:
> >> > > > I currently use this to make sure to always run the external
> command.
> >> > > > Is it the best way to do so? Thanks.
> >> > > >
> >> > > > "$(type -P cmd)" arg1 arg2
> >> > >
> >> > > env cmd arg1 arg2
> >> > > also works
> >> >
> >> > There is also another way too:
> >> >
> >> >   (exec cmd ...)
> >> >
> >> > As I learned just the other day:
> >> >
> >> >   http://lists.gnu.org/archive/html/bug-bash/2018-09/msg00104.html
> >> >   http://lists.gnu.org/archive/html/bug-bash/2018-09/msg00101.html
> >> >
> >> > Bob
> >> >
> >>
> >> This is good. (exec cmd) is faster.
> >>
> >> $  ./main_ls.sh
> >> time for ((i=0;i<300;++i))
> >> do
> >>     (exec ls)
> >> done > /dev/null
> >>
> >> real    0m2.231s
> >> user    0m0.677s
> >> sys    0m1.013s
> >>
> >> time for ((i=0;i<300;++i))
> >> do
> >>     "$(type -P ls)"
> >> done > /dev/null
> >>
> >> real    0m2.742s
> >> user    0m0.830s
> >> sys    0m1.352s
> >> $ cat main_ls.sh
> >> #!/usr/bin/env bash
> >> # vim: set noexpandtab tabstop=2:
> >>
> >> set -v
> >> time for ((i=0;i<300;++i))
> >> do
> >>     (exec ls)
> >> done > /dev/null
> >>
> >> time for ((i=0;i<300;++i))
> >> do
> >>     "$(type -P ls)"
> >> done > /dev/null
> >>
> >>
> >> --
> >> Regards,
> >> Peng
> >
> >
> > You didn't time the env version.
>
> I assume that it is slower as involves an extra external command 'env'.
>
> --
> Regards,
> Peng
>
>
Maybe, maybe not, env is most likely not forking again, so :

* (exec ls):  you have probably a fork and an exec.
* env ls:  you have probably a fork an exec and then another exec.

The expensive operation is (I assume) forking so the results may not be so
different..in fact:

$ cat envexec
time for ((i=0;i<300;++i))
do
    (exec ls)
done > /dev/null

time for ((i=0;i<300;++i))
do
   env ls
done > /dev/null

$ bash envexec

real    0m1,140s
user    0m0,272s
sys     0m0,232s

real    0m1,075s
user    0m0,020s
sys     0m0,072s


reply via email to

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