PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Is << faster than echo?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Is << faster than echo?

Réponse
 
LinkBack Outils de la discussion
Vieux 25/05/2007, 17h28   #1
tmp123
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is << faster than echo?

Hello,

After made the following test:

$ cat foo.sh
#!/usr/bin/bash
#
in="a;b;c"

time echo $in | cut -d ';' -f 2

time cut -d ';' -f 2 <<END
$in
END

$ ./foo.sh
b

real 0m0.046s
user 0m0.045s
sys 0m0.046s
b

real 0m0.032s
user 0m0.030s
sys 0m0.015s


it seems that use of here documents is a few faster than echo and
pipeline.

Knowns someone if this can be taken as a design rule, in the cases
where time is a few important, like inside long loops?

Thanks.

  Réponse avec citation
Vieux 25/05/2007, 17h47   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is << faster than echo?

tmp123 wrote:
> Hello,
>
> After made the following test:
>
> $ cat foo.sh
> #!/usr/bin/bash
> #
> in="a;b;c"
>
> time echo $in | cut -d ';' -f 2
>
> time cut -d ';' -f 2 <<END
> $in
> END
>
> $ ./foo.sh
> b
>
> real 0m0.046s
> user 0m0.045s
> sys 0m0.046s
> b
>
> real 0m0.032s
> user 0m0.030s
> sys 0m0.015s
>
>
> it seems that use of here documents is a few faster than echo and
> pipeline.


Makes sense.

> Knowns someone if this can be taken as a design rule, in the cases
> where time is a few important, like inside long loops?


Using shell builtins is usually (if supported by a shell feature even
/always/?) faster and can as least be a rule of thumb to use builtins
in principal. (There are other issues, like portability, though, that
might be of greater importance in some cases.)

Another, less clumsy (and less portable), way are here strings

time cut -d ';' -f 2 <<< "$in"

which are supported by some of the newer shells (ksh, bash, zsh).

Janis
  Réponse avec citation
Vieux 25/05/2007, 17h50   #3
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is << faster than echo?

Janis Papanagnou wrote:
> tmp123 wrote:
>
>> Hello,
>>
>> After made the following test:
>>
>> $ cat foo.sh
>> #!/usr/bin/bash
>> #
>> in="a;b;c"
>>
>> time echo $in | cut -d ';' -f 2
>>
>> time cut -d ';' -f 2 <<END
>> $in
>> END
>>
>>
>> it seems that use of here documents is a few faster than echo and
>> pipeline.
>> Knowns someone if this can be taken as a design rule, in the cases
>> where time is a few important, like inside long loops?

>
>
> Using shell builtins is usually (if supported by a shell feature even
> /always/?) faster and can as least be a rule of thumb to use builtins
> in principal. (There are other issues, like portability, though, that
> might be of greater importance in some cases.)


Well, echo is also a builtin. So my answer makes not much sense, I fear.
:-/

Janis
  Réponse avec citation
Vieux 25/05/2007, 19h08   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is << faster than echo?

On 2007-05-25, tmp123 wrote:
> Hello,
>
> After made the following test:
>
> $ cat foo.sh
> #!/usr/bin/bash
> #
> in="a;b;c"
>
> time echo $in | cut -d ';' -f 2
>
> time cut -d ';' -f 2 <<END
> $in
> END
>
> $ ./foo.sh
> b
>
> real 0m0.046s
> user 0m0.045s
> sys 0m0.046s
> b
>
> real 0m0.032s
> user 0m0.030s
> sys 0m0.015s
>
>
> it seems that use of here documents is a few faster than echo and
> pipeline.


Both of which are vastly slower than either of:

temp=${in#*;}
echo "${temp%%;*}"

Or:

IFS=\;
set -- $in
echo $2

> Knowns someone if this can be taken as a design rule, in the cases
> where time is a few important, like inside long loops?


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 25/05/2007, 22h51   #5
sjdevnull@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is << faster than echo?

On May 25, 12:28 pm, tmp123 <tmp...@menta.net> wrote:
> Hello,
>
> After made the following test:
>
> $ cat foo.sh
> #!/usr/bin/bash
> #
> in="a;b;c"
>
> time echo $in | cut -d ';' -f 2
>
> time cut -d ';' -f 2 <<END
> $in
> END
>
> $ ./foo.sh
> b
>
> real 0m0.046s
> user 0m0.045s
> sys 0m0.046s
> b
>
> real 0m0.032s
> user 0m0.030s
> sys 0m0.015s
>
> it seems that use of here documents is a few faster than echo and
> pipeline.
>
> Knowns someone if this can be taken as a design rule, in the cases
> where time is a few important, like inside long loops?
>
> Thanks.


Putting both alternatives into a loop of 100 iterations gives me
(first is the "echo" version) 3 runs indicating odd results. "time"
is probably not accounting the parent-shell's here-document to the
subprocess's user/sys time, since I'm consistently seeing lower user/
sys for the here-document version but higher wall times.
[sumner@localhost sumner]$ bash t.sh

real 0m1.038s
user 0m0.085s
sys 0m0.203s

real 0m1.334s
user 0m0.072s
sys 0m0.203s
[sumner@localhost sumner]$ bash t.sh

real 0m1.158s
user 0m0.075s
sys 0m0.213s

real 0m1.767s
user 0m0.069s
sys 0m0.210s
[sumner@localhost sumner]$ bash t.sh

real 0m0.528s
user 0m0.082s
sys 0m0.210s

real 0m1.341s
user 0m0.074s
sys 0m0.202s

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,55700 seconds with 13 queries