【问题标题】:Why is string format so much faster than converting something into a string in Python? [duplicate]为什么字符串格式比在 Python 中将某些内容转换为字符串要快得多? [复制]
【发布时间】:2021-11-13 07:04:57
【问题描述】:

我正在编写一个 Python 程序,我必须在代码中的某处将一些繁重的数据转换为字符串。但后来我进行了一些测试,看看什么是最好的方法。想了两个办法,用timeit做了一些测试:

我的问题是:为什么字符串格式化比调用str() 更快?

【问题讨论】:

    标签: python string performance type-conversion


    【解决方案1】:

    如果你在 f-string 和 str() 上都使用反汇编模块,你会看到 str() 调用了一个额外的函数并且函数调用非常耗时!

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 1970-01-01
      • 2016-12-26
      • 2020-12-05
      • 2019-03-14
      • 2019-04-01
      • 2018-08-28
      • 2013-01-03
      • 1970-01-01
      相关资源
      最近更新 更多