Despite this question Why is ''.join() faster than += in Python? and it's answers and this great explanation of the code behind the curtain: https://paolobernardi.wordpress.com/2012/11/06/python-string-concatenation-vs-list-join/
My tests suggest otherwise and I am baffled.
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.
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)
(1473524757.681939, 1473524757.68521, '=', 0.0032711029052734375)
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)
(1473524814.544177, 1473524814.547544, '=', 0.0033669471740722656)
import time
x=10000*"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
y=""
t1 = (time.time())
y= "".join(x)
t2 = (time.time())
#print (y)
print (t1,t2,"=",t2-t1)
(1473524861.949515, 1473524861.978755, '=', 0.029239892959594727)
As can be seen the "".join()
is much slower and yet we're told that it's meant to be quicker.
These values are very similar in both python2.7 and python3.4
Edit: Ok fair enough.
The "one huge string" thing is the kicker.
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)
(1473526344.55748, 1473526344.558409, '=', 0.0009288787841796875)
An order of magnitude quicker. Mea Culpa!