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()

python 字符串拼接效率打脸帖

 

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

 

相关文章:

  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2021-07-18
  • 2021-12-28
猜你喜欢
  • 2022-12-23
  • 2021-07-29
  • 2022-01-21
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
相关资源
相似解决方案