为什么字符串格式比在 Python 中将某些内容转换为字符串快得多?

Why is string format so much faster than converting something into a string in Python?

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

进行了一些测试

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

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