TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Efficient String Concatenation in Python

78 pointsby cvursacheover 11 years ago

10 comments

inglespover 11 years ago
Note that since this article was written (2004) CPython performs an in-place optimisation for assignments of the form<p><pre><code> s1 += s2 </code></pre> There are details at point six of <a href="http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange" rel="nofollow">http:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;library&#x2F;stdtypes.html#sequence-type...</a>, where it also says that str.join() is preferable.
评论 #6347490 未加载
评论 #6346539 未加载
1st1over 11 years ago
A very old article from 2004 (please edit the title). The tests are only for cpython2 and for a very old one.
kingkilrover 11 years ago
If you really care about efficiency (and are using PyPy), an actually efficient way to concat strings:<p><pre><code> from __pypy__.builders import StringBuilder builder = StringBuilder(&lt;size estimate, optional&gt;) for i in xrange(10000): builder.append(str(i)) return builder.build()</code></pre>
zeebooover 11 years ago
Note these tests were run on Python 2.2 which makes the results old enough to almost certainly be invalid. Last time I checked, straight string concatenation was faster than everything else for the types of inputs I was dealing with.
评论 #6346656 未加载
kstrauserover 11 years ago
How would a method 7 using generator expressions fare on the same system, like:<p><pre><code> return &#x27;&#x27;.join(`num` for num in xrange(loop_count)) </code></pre> On one hand, it avoids creating a temporary list in memory. On the other, it can&#x27;t know in advance how long the final output of the loop will be and so couldn&#x27;t use tricks like preallocating enough RAM.
评论 #6346330 未加载
评论 #6346367 未加载
gamegoblinover 11 years ago
I believe that the current most efficient way of doing this is<p><pre><code> &#x27;&#x27;.join(map(str, range(n)))</code></pre>
评论 #6346646 未加载
acjohnson55over 11 years ago
How have I never heard of backtick quotes? Apparently they are an abomination [1], so I will promptly force-forget them.<p>[1] <a href="http://stackoverflow.com/a/7490311/807674" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;7490311&#x2F;807674</a>
dbeckerover 11 years ago
Doing stuff efficiently is great when there&#x27;s some benefit from efficiency. But I&#x27;ve seen a lot of fragile code (and implementation specific hacks) created based on benchmarks, when the time savings were meaningless in practice.
mappuover 11 years ago
For comparison, PHP has mutable strings. Some totally unscientific averaged benchmarks (Atom D2700, PHP 5.4.4-14 amd64):<p><pre><code> $buff = &#x27;&#x27;; for ($i = 0; $i != 100000; ++$i) { $buff .= $i; } </code></pre> Runs in about 0.06s<p><pre><code> $buff = array(); for ($i = 0; $i != 100000; ++$i) { $buff[] = $i; } $ret = implode(&#x27;&#x27;, $buff); </code></pre> Runs slower, in about 0.10s<p><pre><code> $ret = implode(&#x27;&#x27;, range(0, 100000)); </code></pre> Takes roughly the same time, 0.10s
评论 #6347304 未加载
Sami_Lehtinenover 11 years ago
How about using Python 3.3.2?<p>I did many kind of performance tests when implementing my PyClockPro caching lib, even if I don&#x27;t use strings in it.