|
Home > Archive > Unix Shell > May 2007 > Is << faster than echo?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is << faster than echo?
|
|
| tmp123 2007-05-25, 1:19 pm |
| 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.
| |
| Janis Papanagnou 2007-05-25, 1:19 pm |
| 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
| |
| Janis Papanagnou 2007-05-25, 1:19 pm |
| Janis Papanagnou wrote:
> tmp123 wrote:
>
>
>
> 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
| |
| sjdevnull@yahoo.com 2007-05-25, 7:17 pm |
| 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
| |
| Chris F.A. Johnson 2007-05-26, 7:21 am |
| 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
|
|
|
|
|