$ python -mtimeit -s"from math import sqrt; x = 123" "x**.5"
1000000 loops, best of 3: 0.445 usec per loop
$ python -mtimeit -s"from math import sqrt; x = 123" "sqrt(x)"
1000000 loops, best of 3: 0.574 usec per loop
$ python -mtimeit -s"import math; x = 123" "math.sqrt(x)"
1000000 loops, best of 3: 0.727 usec per loop
from http://stackoverflow.com/questions/842245/which-is-more-accurate-x-5-or-math-sqrtx
本文通过使用Python的timeit模块对比了不同方法计算平方根的性能:直接使用指数0.5的方法、从math模块导入sqrt函数的方法以及直接调用math.sqrt的方法。测试结果显示,在相同的迭代次数下,直接使用指数0.5的方法速度最快。
398

被折叠的 条评论
为什么被折叠?



