我对用于代码优化的 timit 函数有疑问。例如,我在文件中编写带有参数的函数,我们称它为myfunctions.py
包含:
def func1(X):
Y = X+1
return Y
我在第二个文件中测试这个函数,test.py
我在其中调用计时器函数来测试代码性能(显然是更复杂的问题!)包含:
import myfunctions
X0 = 1
t = Timer("Y0 = myfunctions.func1(X0)")
print Y0
print t.timeit()
没有计算Y0
,即使我注释print Y0
行也会发生错误global name 'myfunctions' is not defined
。
如果我使用命令指定设置
t = Timer("Y0 = myfunctions.func1(X0)","import myfunctions")
现在发生了错误global name 'X0' is not defined
。
有人知道如何解决这个问题吗?非常感谢。