Speeding up: s += "string"
marcus at infa.abo.fi
Thu Apr 17 17:54:37 CEST 2003
On Tue, 15 Apr 2003 07:56:21 -0400, Roy Smith <roy at panix.com> wrote:
>I certainly agree that mutable strings would be useful in some
>situations. For example, in the quadratic run-time program I mentioned
>in a parallel thread, I could easily see doing:
>s = ""
>s.reserve (len (line))
>for word in line.split():
> blah blah blah
> s += stuff
>and being very happy that I was now getting linear run time with only a
>trivial change in logic.
>The problem is, strings are currently documented as being immutable.
>Making them mutable would break existing code. I can, for example, find
>the length of a string and use that length someplace later in my
>program, knowing that it hasn't changed. Not to mention using strings
>as dictionary keys.
It would be nice if a Python optimizer of sorts could be developed to
create the above code (or, rather, behaviour) given the following
s = ""
for word in line.split():
s += stuff
I'm not a Python developer, but I _think_ that this problem generally
could be hard/impossible to fix, given the extremely dynamic nature of
Python. So any solution would need to "nail down" some things in
Python, and remove some of its dynamic nature. The question would then
be if this is considered "premature optimization".
I'd love to be told otherwise, though. If a feasible solution exists,
more power to you :)
maalanen at abo.fi
More information about the Python-list