best way to replace first word in string?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Oct 23 05:56:54 EDT 2005


On Sun, 23 Oct 2005 01:30:36 -0400, Mike Meyer wrote:

>> At least, that's what it looks like to me -- I'm perplexed by the *vast*
>> increase in speed in your version, far more than I would have predicted
>> from pulling out the char conversion. I can think of three
>> possibilities:
> 
> Everything got faster, so it wasn't just pulling the chr conversion.

Sure -- I'm not concerned about proportional speed increases.
 
>> (1) Your PC is *hugely* faster than mine;
> 
> It's a 3Ghz P4.

Perhaps a tad faster, but not too much.
 
>> (2) Your value of x is a lot smaller than I was using (you don't actually
>> say what x you use); or
> 
> It's still in the buffer, and I copied it from your timings:
> 
>>>> x = 100000
> 
>> (3) You are using a version and/or implementation of Python that has a
>> different underlying implementation of string concatenation.
> 
> I'm runing Python 2.4.1 built with GCC 3.4.2.

There is a difference there:

http://www.python.org/doc/2.4/whatsnew/node12.html

Second-last paragraph:

[quote]
String concatenations in statements of the form s = s + "abc" and s +=
"abc" are now performed more efficiently in certain circumstances. This
optimization won't be present in other Python implementations such as
Jython, so you shouldn't rely on it; using the join() method of strings is
still recommended when you want to efficiently glue a large number of
strings together. (Contributed by Armin Rigo.)
[end quote]


-- 
Steven.




More information about the Python-list mailing list