On Tuesday 24 October 2006 17:49, Markus Gritsch wrote:
> Since Python string objects are immutable, a new instance of the
> object has to be created, which can become expensive, if the strings
> become quite long.  A performant way to concatenate Strings is to add
> the strings to a list, and join the list at the end.  Executing the
> following script on my computer using Python 2.4 for 100000 iterations
> takes 0.063 vs. 0.062 seconds, so no big difference, but when
> performing 1000000 iterations the difference becomes quite large:
> 6.125 vs. 0.532 seconds, so joining the list is more than 10 times
> faster.  Things get worse for string concatenation with larger
> strings.

The discussion and comparison was between string concatenation and the % 
operator (which apply to creating strings from a small number of other 
elements), not the join method which has other semantics (combining many 
strings together). I doubt that anyone would try to concatenate 1000000 
strings by appending them one to another (well, I wouldn't actually bet 
on this ;) but you got my point), or by trying to use the % operator.

But since you mentioned it, I have obtained different results by running 
your test:

dawn:~$ python2.4 a.py
1.04168891907
0.631967067719

which means that string concatenation was only 40% slower, not 10 times 
slower for me. Now considering the way this test was setup I'd say it is 
extremely biased. The 1st method creates/destroys 1000000 string objects, 
while the 2nd only creates 1 at the end. Also the 2nd method doesn't even 
add new objects to the list, it keeps appending a reference to the same 
string, which means in the second case you only have 3 objects overall: 
the list, the 'hello' string and the result string.
Given these conditions, I'm amazed that the 1st case was only 40% slower.

If you really want to compare them 1:1 you should execute the same number 
of operations overall (i.e. as many joins as concatenations).

-- 
Dan

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to