Re: String concatenation performance with +=

2009-02-14 Thread Steven D'Aprano
Nick Craig-Wood wrote: > The optimized += depends on their being no other references to the > string.  Strings are immutable in python.  So append must return a new > string.  However the += operation was optimised to do an in-place > append if and only if there are no other references to the stri

Re: String concatenation performance with +=

2009-02-14 Thread Nick Craig-Wood
Sammo wrote: > String concatenation has been optimized since 2.3, so using += should > be fairly fast. > > In my first test, I tried concatentating a 4096 byte string 1000 times > in the following code, and the result was indeed very fast (12.352 ms > on my machine). > > import time > t =

Re: String concatenation performance with +=

2009-02-14 Thread Sammo
On Feb 14, 5:33 pm, Steven D'Aprano wrote: > > AFAIK, using list mutation and "".join only improves performance if > > the "".join is executed outside of the loop. > > Naturally. If you needlessly join over and over again, instead of delaying > until the end, then you might as well do string conca

Re: String concatenation performance with +=

2009-02-14 Thread Sammo
On Feb 14, 4:47 pm, Steven D'Aprano wrote: > > Sammo gmail.com> writes: > >> String concatenation has been optimized since 2.3, so using += should > >> be fairly fast. > > > This is implementation dependent and shouldn't be relied upon. > > It's also a fairly simple optimization and really only a

Re: String concatenation performance with +=

2009-02-13 Thread Steven D'Aprano
Sammo wrote: > Okay, this is what I have tried for string concatenation: > > 1. Using += implemented using simple operations (12 ms) > 2. Using += implemented inside a class (4000+ ms) > 3. Using "".join implemented using simple operations (4000+ ms) > 4. Using "".join implemented inside a class

Re: String concatenation performance with +=

2009-02-13 Thread Steven D'Aprano
Steven D'Aprano wrote: > Benjamin Peterson wrote: > >> Sammo gmail.com> writes: >> >>> String concatenation has been optimized since 2.3, so using += should >>> be fairly fast. >> >> This is implementation dependent and shouldn't be relied upon. > > It's also a fairly simple optimization and

Re: String concatenation performance with +=

2009-02-13 Thread Steven D'Aprano
Benjamin Peterson wrote: > Sammo gmail.com> writes: > >> String concatenation has been optimized since 2.3, so using += should >> be fairly fast. > > This is implementation dependent and shouldn't be relied upon. It's also a fairly simple optimization and really only applies to direct object a

Re: String concatenation performance with +=

2009-02-13 Thread Sammo
Okay, this is what I have tried for string concatenation: 1. Using += implemented using simple operations (12 ms) 2. Using += implemented inside a class (4000+ ms) 3. Using "".join implemented using simple operations (4000+ ms) 4. Using "".join implemented inside a class (4000+ ms) On Feb 14, 3:1

Re: String concatenation performance with +=

2009-02-13 Thread Benjamin Peterson
Sammo gmail.com> writes: > > String concatenation has been optimized since 2.3, so using += should > be fairly fast. This is implementation dependent and shouldn't be relied upon. > > Note that I need to do something to mydata INSIDE the loop, so please > don't tell me to append moredata to a

String concatenation performance with +=

2009-02-13 Thread Sammo
String concatenation has been optimized since 2.3, so using += should be fairly fast. In my first test, I tried concatentating a 4096 byte string 1000 times in the following code, and the result was indeed very fast (12.352 ms on my machine). import time t = time.time() mydata = "" moredata = "A"