473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String concatenation performance

I was just reading a "Python Speed/Performance Tips" article on the
Python wiki
http://wiki.python.org/moin/PythonSpeed/PerformanceTips
and I got to the part that talks about string concatenation and that it
is faster when using join instead of += because of strings being
immutable. So I have tried it:

from time import time
t=time()

s='almfklasmfkmaskmkmasfkmkqemkmqeqw'
for x in range(40):
#s+= s[len(s)/2:]
s="".join((s,s[len(s)/2:]))

print 'duration', time() - t

And I get 1.55016708374 for the concatenation and 3.01116681099 for the
join. I have also tried to put the join outside but it is still a
little bit over 3.
I'm using Python 2.4.2, GCC 3.3.3 (SuSE Linux).

So what am I doing wrong ?

May 11 '06 #1
4 2101
Cristian.Codorean wrote:
I was just reading a "Python Speed/Performance Tips" article on the
Python wiki
http://wiki.python.org/moin/PythonSpeed/PerformanceTips
and I got to the part that talks about string concatenation and that
it is faster when using join instead of += because of strings being
immutable.
.... snip ...
So what am I doing wrong ?


What you are doing wrong is failing to search the newsgroup to see if
anyone else has asked the same question within the last few days. In
particular see the thread titled 'which is better, string concatentation
or substitution?' message <44*********************@news.astraweb.com>

http://groups.google.co.uk/group/com...412fc21aa1fe8c
May 11 '06 #2
Cristian.Codorean wrote:
I was just reading a "Python Speed/Performance Tips" article on the
Python wiki
http://wiki.python.org/moin/PythonSpeed/PerformanceTips
and I got to the part that talks about string concatenation and that it
is faster when using join instead of += because of strings being
immutable.


The idea is that you call join() once rather than calling += many
times. You achieve this by placing all the strings you want
concatenating into a single list, not by calling join() with multiple
lists.

--
Ben Sizer

May 11 '06 #3
Cristian.Codorean a écrit :
I was just reading a "Python Speed/Performance Tips" article on the
Python wiki
http://wiki.python.org/moin/PythonSpeed/PerformanceTips
and I got to the part that talks about string concatenation and that it
is faster when using join instead of += because of strings being
immutable.
This is somewhat obsolete. String concatenation have been subject to
some optimization since 2.3.x (IIRC - else please someone correct me).
NB: this is only true for CPython.

But the "".join() idiom is, well, still idiomatic...
So I have tried it:

from time import time
t=time()

s='almfklasmfkmaskmkmasfkmkqemkmqeqw'
for x in range(40):
#s+= s[len(s)/2:]
s="".join((s,s[len(s)/2:]))
Lol...

I'm afraid you didn't get the idiom right. The point is to avoid useless
allocations in the loop body. The idiom is:

buf = []
for x in range(42):
buf.append(s)
s = "".join(buf)
print 'duration', time() - t


timeit may be a better choice for microbenchmarks.

May 11 '06 #4
Thanks guys ! Growing and learning :)

May 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
15
by: James | last post by:
Which is better, which is faster, which is easier etc... ????? String.Format ( "yadda {0} yadda {1}", x, y ) "yadda" + x.ToString() + " yadda" + y.tostring(); My code has a mish mash of...
3
by: Author | last post by:
I have always been wondering if there is any significant different between doing System.Console.WriteLine("Employee Name = " + employee.FirstName + " " + employee.LastName); and ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.