Speeding up: s += "string"
Jp Calderone
exarkun at intarweb.us
Fri May 9 16:00:00 EDT 2003
On Thu, May 08, 2003 at 02:47:08PM -0700, Erik Max Francis wrote:
> Beat Bolli wrote:
>
> > When I'm really concerned about speed, I pre-bind the append method:
> >
> > lst = []
> > add = lst.append
> > for many times:
> > add(stuff)
> > return ''.join(lst)
> >
> > You could also define a global concat function:
> >
> > concat = ''.join
> >
> > and use this in the last line above.
>
> The expense here is not in the calling of the function, it's in the time
> it takes the function to run. Repeatedly using
Creating and calling a bound method object is expensive too (about 30%
more expensive than calling a regular function, iirc). Beat Bolli's
solution moves this overhead outside the loop, instead of having Python
create a new bound method object for every iteration.
It can yield noticable speedups, and I've used it myself here and there.
Jp
--
A disciple of another sect once came to Drescher as he was eating his
morning meal. "I would like to give you this personality test," said the
outsider, "because I want you to be happy." Drescher took the paper that was
offered him and put it into the toaster: "I wish the toaster to be happy,
too."
--
up 7 days, 16:38, 8 users, load average: 0.00, 0.00, 0.00
More information about the Python-list
mailing list