对比字符串连接速度,可以发现, 字符串少时, +快;
字符串多时, join快
from time import time
def method1():
t = time()
for i in range():
s = 'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'
t1 = time()
print ("Time %f" % (t1 - t))
def method2():
t = time()
for i in range():
s = ''.join(['hiweeds','hiweeds','hiweeds','hiweeds','hiweeds','hiweeds','hiweeds','hiweeds','hiweeds','hiweeds'])
print (time() - t)
print ('hello World')
method1()
method2()
def method3():
t = time()
for i in range():
s = 'hiweeds'+'hiweeds'+'hiweeds'+'hiweeds'
t1 = time()
print ("Time %f" % (t1 - t))
def method4():
t = time()
for i in range():
s = ''.join(['hiweeds','hiweeds','hiweeds','hiweeds'])
print (time() - t)
print ('hello World')
method3()
method4()