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毫秒,不是性能程序真没必要纠结哪种拼接靠谱。哪种顺手,就上哪种。

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/jackicalSong/p/8303615.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!