Python timeit 设置中的局部变量
Local variables in Python timeit setup
在我阅读有关 timeit 的所有地方,我发现我只能以这种方式使用变量:
s1 = 'abc'
s2 = 'abc'
timeit.timeit('s1==s2', 'from __main__ import s1, s2', number=10**4)
或
s1 = 'abc'
s2 = 'abc'
def func():
timeit.time('s1==s2', 'from __main__ import s1,s2', number=10**4)
这意味着只要变量在主程序中,您也可以在函数中使用timeit.timeit。
我想将 timeit.timeit 与它所在范围内的变量一起使用,例如 :
def func():
s1 = 'abc'
s2 = 'abc'
timeit.timeit(...)
如您所见,我的问题是:
当它们都不在主程序中时,如何将 timeit.timeit 用于同一范围内的变量?
正如 jonrsharpe 所解释的,timeit
范围内的函数 运行 没有(直接的)方法来访问 something outside its scope that is not a global。
您应该考虑重写您的函数以将它需要使用的变量作为参数 - 使用全局变量通常被认为是一种不好的做法,会导致很多问题。
为了向 timeit.timeit
提供参数,您可以使用 partial function:
from functools import partial
def func(s1,s2):
pass
timeit.timeit( partial( func, s1='bla', s2='bla' ) )
I would like to use timeit.timeit with variables that are within the scope it is in.
TLDR:
使用 lambda
闭包(如此称呼是因为它关闭了函数中的变量):
def func():
s1 = 'abc'
s2 = 'abc'
return timeit.timeit(lambda: s1 == s2)
我认为这正是您所要求的。
>>> func()
0.12512516975402832
说明
所以在全局范围内,你想使用全局变量,而局部变量,本地变量?在全局范围内,locals()
returns 与 globals()
相同,因此您 ', '.join(locals())
并将其粘贴在 'from __main__ import '
或 [=24= 的末尾] 因为它们在全局范围内是等效的:
>>> s1 = 'abc'
>>> s2 = 'abc'
>>> timeit.timeit('s1==s2', 'from __main__ import ' + ', '.join(globals()))
0.14271061390928885
你也可以用函数和 globals()
来做到这一点,但你不能使用 locals():
s1 = 'abc'
s2 = 'abc'
def func():
return timeit.timeit('s1==s2', 'from __main__ import ' + ', '.join(globals()))
和
>>> func()
0.14236921612231157
但以下内容不起作用,因为您必须从 import 语句访问隐藏在函数局部范围内的变量:
def func():
s1 = 'abc'
s2 = 'abc'
return timeit.timeit('s1==s2', 'from __main__ import ' + ', '.join(locals()))
但是因为您可以简单地将函数传递给 timeit,所以您可以做的是:
def func(s1='abc', s2='abc'):
s1 == s2
和
>>> timeit.timeit(func)
0.14399981498718262
所以这也意味着,在您的函数中,您可以为 timeit 提供一个 lambda 闭包:
def func():
s1 = 'abc'
s2 = 'abc'
return timeit.timeit(lambda: s1 == s2)
或全功能定义:
def func():
s1 = 'abc'
s2 = 'abc'
def closure():
return s1 == s2
return timeit.timeit(closure)
我认为这正是您所要求的。
>>> func()
0.12512516975402832
When they're both not in the main program
如果您想从 __main__
以外的其他模块加入全局变量而不是设置,请使用:
'from ' + __name__ + ' import ' + ', '.join(globals())
在我阅读有关 timeit 的所有地方,我发现我只能以这种方式使用变量:
s1 = 'abc'
s2 = 'abc'
timeit.timeit('s1==s2', 'from __main__ import s1, s2', number=10**4)
或
s1 = 'abc'
s2 = 'abc'
def func():
timeit.time('s1==s2', 'from __main__ import s1,s2', number=10**4)
这意味着只要变量在主程序中,您也可以在函数中使用timeit.timeit。 我想将 timeit.timeit 与它所在范围内的变量一起使用,例如 :
def func():
s1 = 'abc'
s2 = 'abc'
timeit.timeit(...)
如您所见,我的问题是:
当它们都不在主程序中时,如何将 timeit.timeit 用于同一范围内的变量?
正如 jonrsharpe 所解释的,timeit
范围内的函数 运行 没有(直接的)方法来访问 something outside its scope that is not a global。
您应该考虑重写您的函数以将它需要使用的变量作为参数 - 使用全局变量通常被认为是一种不好的做法,会导致很多问题。
为了向 timeit.timeit
提供参数,您可以使用 partial function:
from functools import partial
def func(s1,s2):
pass
timeit.timeit( partial( func, s1='bla', s2='bla' ) )
I would like to use timeit.timeit with variables that are within the scope it is in.
TLDR:
使用 lambda
闭包(如此称呼是因为它关闭了函数中的变量):
def func():
s1 = 'abc'
s2 = 'abc'
return timeit.timeit(lambda: s1 == s2)
我认为这正是您所要求的。
>>> func()
0.12512516975402832
说明
所以在全局范围内,你想使用全局变量,而局部变量,本地变量?在全局范围内,locals()
returns 与 globals()
相同,因此您 ', '.join(locals())
并将其粘贴在 'from __main__ import '
或 [=24= 的末尾] 因为它们在全局范围内是等效的:
>>> s1 = 'abc'
>>> s2 = 'abc'
>>> timeit.timeit('s1==s2', 'from __main__ import ' + ', '.join(globals()))
0.14271061390928885
你也可以用函数和 globals()
来做到这一点,但你不能使用 locals():
s1 = 'abc'
s2 = 'abc'
def func():
return timeit.timeit('s1==s2', 'from __main__ import ' + ', '.join(globals()))
和
>>> func()
0.14236921612231157
但以下内容不起作用,因为您必须从 import 语句访问隐藏在函数局部范围内的变量:
def func():
s1 = 'abc'
s2 = 'abc'
return timeit.timeit('s1==s2', 'from __main__ import ' + ', '.join(locals()))
但是因为您可以简单地将函数传递给 timeit,所以您可以做的是:
def func(s1='abc', s2='abc'):
s1 == s2
和
>>> timeit.timeit(func)
0.14399981498718262
所以这也意味着,在您的函数中,您可以为 timeit 提供一个 lambda 闭包:
def func():
s1 = 'abc'
s2 = 'abc'
return timeit.timeit(lambda: s1 == s2)
或全功能定义:
def func():
s1 = 'abc'
s2 = 'abc'
def closure():
return s1 == s2
return timeit.timeit(closure)
我认为这正是您所要求的。
>>> func()
0.12512516975402832
When they're both not in the main program
如果您想从 __main__
以外的其他模块加入全局变量而不是设置,请使用:
'from ' + __name__ + ' import ' + ', '.join(globals())