python 字符串拼接效率打脸帖

https://www.cnblogs.com/chenjingyi/p/5741901.html

这篇博客写的好,字符串并不是+ 效率就一定比 "%" % ('a') 就低。

按照博主写的代码我再验证了下,果然“啪啪”打脸真响。

 

from time import time

def method1():
    t=time()
    for i in range(10000):
        s="jackical123"+"jackical123"+"jackical123"+"jackical123"
    print(time()-t)

def method2():
    t=time()
    for i in range(10000):
        s="%s%s%s%s" % ('jackical123','jackical123','jackical123','jackical123')
    print(time()-t)
    
method1()
method2()

 

小量变量拼接还是 + 号靠谱,而且循环1万遍,花费3毫秒,不是性能程序真没必要纠结哪种拼接靠谱。哪种顺手,就上哪种。

 

posted on 2018-01-17 16:26  我叫宋  阅读(270)  评论(0编辑  收藏  举报

导航