Why it is so dramatical?

Peter Hansen peter at engcorp.com
Mon Sep 2 18:52:01 EDT 2002

djw wrote:
> holger krekel wrote:
>> If you have something like this in a loop it's often
>> *much* better to work with a list and later do a
>>     "".join(stringlist)
>> As you observed yourself this is around 100-1000 times faster. 
>> And consider: if strings were not immutable but modified in-place
>> (like lists) you couldn't use them as dictionary keys.
>> if-it-hurts-don't-do-it-ly, yours Holger
> Stupid question:

It's only a stupid question if you didn't expect this relatively common
answer to such questions:  "feel free to contribute a patch"... :-)

> If ""join() is 100-1000 X faster, why doesn't the Python interperter 
> translate s = s1 + s2 + ... into s = "".join( [ s1, s2, ... ] ) 
> automatically?

In cases like this, you need to know that Python is so dynamic that
the compiler cannot know that the items being added are strings until
runtime.  The "".join() convention does not work well if the items in
the sequence are not already strings.

If you feel compelled to suggest that the compiler could at least do
that for constant strings, see my first point above. :-)


More information about the Python-list mailing list