bug-bash
[Top][All Lists]
Advanced

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

Re: for; do; done regression ?


From: Greg Wooledge
Subject: Re: for; do; done regression ?
Date: Mon, 10 Jan 2011 08:38:41 -0500
User-agent: Mutt/1.4.2.3i

On Fri, Jan 07, 2011 at 04:17:17AM +0300, Alexander Tiurin wrote:
> ~$ time for i in `seq 0 10000`  ; do echo /o/23/4 | cut -d'/' -f2 ; done
> > /dev/null 

Others have already begun investigating the internal causes of the
slowdown.  But if you actually want this particular piece of code to
run more quickly, you can optimize it quite a bit:

x=/o/23/4
for ((i=0; i<=10000; i++)); do tmp=${x#*/}; echo "${tmp%%/*}"; done

There's no need for all those forks you were doing.

(And then you're going to say "But this was just an example, and my real
code is more complex."  And you still won't show the real code.  And
we'll get in a big fight.  And everyone will be sad.  So all I can really
do for you is give general advice, like "Don't use echo|cut when you can
use parameter expansion.  Don't use seq, ever, because it's Linux-only
and is a stupid way to count when bash has builtin integer arithmetic.")

P.S.,

imadev:~$ bash-4.1.9
imadev:~$ x=/o/23/4    
imadev:~$ time for ((i=0; i<=10000; i++)); do tmp=${x#*/}; echo "${tmp%%/*}"; 
done >/dev/null

real    0m2.080s
user    0m2.020s
sys     0m0.040s
imadev:~$ time for ((i=0; i<=10000; i++)); do tmp=${x#*/}; echo "${tmp%%/*}"; 
done >/dev/null

real    0m2.082s
user    0m2.020s
sys     0m0.050s
imadev:~$ time for ((i=0; i<=10000; i++)); do tmp=${x#*/}; echo "${tmp%%/*}"; 
done >/dev/null

real    0m2.118s
user    0m2.050s
sys     0m0.050s



reply via email to

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