Why does "".join() appear to be slower than +=
<p>Despite this question <a href="http://stackoverflow.com/questions/39312099/why-is-join-faster-than-in-python">Why is &#39;&#39;.join() faster than += in Python?</a> and it's answers and this great explanation of the code behind the curtain: <a href="https://paolobernardi.wordpress.com/2012/11/06/python-string-concatenation-vs-list-join/" rel="nofollow">https://paolobernardi.wordpress.com/2012/11/06/python-string-concatenation-vs-list-join/</a><br>
My tests suggest otherwise and I am baffled.<br>
Am I doing something simple, incorrectly? I'll admit that I'm fudging the creation of x a bit but I don't see how that would affect the outcome.   </p>

<pre><code>import time
x="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
y=""
t1 = (time.time())
for i in range(10000):
    y+=x
t2 = (time.time())
#print (y)
print (t1,t2,"=",t2-t1)
</code></pre>

<p>(1473524757.681939, 1473524757.68521, '=', 0.0032711029052734375)</p>

<pre><code>import time
x="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
y=""
t1 = (time.time())
for i in range(10000):
    y=y+x
t2 = (time.time())
#print (y)
print (t1,t2,"=",t2-t1)
</code></pre>

<p>(1473524814.544177, 1473524814.547544, '=', 0.0033669471740722656)</p>

<pre><code>import time
x=10000*"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
y=""
t1 = (time.time())
y= "".join(x)
t2 = (time.time())
#print (y)
print (t1,t2,"=",t2-t1)
</code></pre>

<p>(1473524861.949515, 1473524861.978755, '=', 0.029239892959594727)</p>

<p>As can be seen the <code>"".join()</code> is much slower and yet we're told that it's meant to be quicker.<br>
These values are very similar in both python2.7 and python3.4</p>

<p><strong>Edit:</strong>
Ok fair enough.</p>

<p>The "one huge string" thing is the kicker.</p>

<pre><code>import time
x=[]
for i in range(10000):
    x.append("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
y=""
t1 = (time.time())
y= "".join(x)
t2 = (time.time())
#print (y)
print (t1,t2,"=",t2-t1)
</code></pre>

<p>(1473526344.55748, 1473526344.558409, '=', 0.0009288787841796875)</p>

<p>An order of magnitude quicker.
Mea Culpa!</p>

<p>You are not comparing the same operation because your first operation added the long string every iteration while join added every item of the string seperatly. (See also @MartijnPieters answer)</p>

<p>If I run a comparison I get completly different timings suggesting that <code>str.join</code> is much faster:</p>

<pre><code>x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

def join_inplace_add(y, x, num):
    for _ in range(num):
        y += x
    return y

def join_by_join(x, num):
    return ''.join([x for _ in range(num)])

%timeit join_by_join('', x, 1000)
# 10000 loops, best of 3: 91 Âµs per loop
%timeit join_inplace_add(x, 1000)
# 1000 loops, best of 3: 325 Âµs per loop
</code></pre>

python