如何让Python显示产生答案所需的迭代次数?

时间:2015-06-06 20:09:13

标签: python

我试图找到Python生成函数根的二分数。例如,如果我有代码:

import math
a=1
b=2
def f(x):
    return x**3+x-7
while b-a>0.001:
    c=(a+b)/2
    if f(a)*f(c)>0:
         a=c
    else:
         b=c
print(c)

此代码只会产生所需的答案。我想知道Python执行二分法的次数以及每次的值是多少。

1 个答案:

答案 0 :(得分:2)

您可以使用某些分析器。例如 line_profiler 。你可以简单地安装它:

pip install line_profiler

看这里: How can I profile python code line-by-line?

相关问题