小编Luc*_*lla的帖子

python中的str性能

在分析一段python代码(python 2.6最多3.2)时,我发现 str将对象(在我的情况下是整数)转换为字符串的方法比使用字符串格式慢几个数量级.

这是基准

>>> from timeit import Timer
>>> Timer('str(100000)').timeit()
0.3145311339386332
>>> Timer('"%s"%100000').timeit()
0.03803517023435887
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样?我错过了什么吗?

python string performance python-2.7 python-3.x

87
推荐指数
2
解决办法
2788
查看次数

标签 统计

performance ×1

python ×1

python-2.7 ×1

python-3.x ×1

string ×1