Adding to the front of a line

Smoot Carl-Mitchell smoot at tic.com
Wed Jan 21 20:40:18 UTC 2009


On Wed, 2009-01-21 at 20:39 +0100, Florian Diesch wrote:

> > On Tue, Jan 20, 2009 at 10:43 PM, Matthew Flaschen
> > <matthew.flaschen at gatech.edu> wrote:
> >> In other words, the sequence is used as input, but not timed.
> >
> > I see. Well, in that case, the difference between using a while read
> > vs a sed is dramatic:
> >
> > dfox at ubuntu:~$ time (while read lines; do echo "127.0.0.1 " $lines;
> > done) <lines >newlines
> >
> > real	0m37.781s
> > user	0m29.542s
> > sys	0m7.732s
> 
> I didn't expect bash beeing *that* much slower than sh:

If you really want to amuse yourself, try running these:

strace -c -f bash -c 'seq 1 1000000 | while read site; do echo "127.0.0.1 $site"; done > /dev/null' 2>trace1

strace -c -f bash -c 'seq 1 1000000 | sed "s/^/127.0.0.1 /" > /dev/null' 2>trace2

You will find that the number of read system calls used by the while
read.. version is significantly greater than the number of read calls
used by the sed version.  Be warned, the first one takes a long time to
trace (several minutes).  If you leave off the -c option, you will see
the first version is reading a single character at a time from the input
stream.  This likely accounts for much of the extra time taken in
processing.  The sed version slurps up the input in 4K blocks which is
considerably more efficient.

None of this really matters much if your input file is a few dozen or
even hundreds of lines long.
-- 
Smoot Carl-Mitchell
Computer Systems and
Network Consultant
smoot at tic.com
+1 480 922 7313
cell: +1 602 421 9005




More information about the ubuntu-users mailing list