简体   繁体   English

在python中导入timeit.timeit变量

[英]timeit.timeit variable importing in python

I am trying to use timeit.timeit() in order to find how much time it takes to execute a specific line of code.我正在尝试使用timeit.timeit()来查找执行特定代码行所需的时间。 The problem is that this line includes variables and I need to import them somehow, so my question is how?问题是这一行包含变量,我需要以某种方式导入它们,所以我的问题是如何? In order to be more clear, the code looks something like this:为了更清楚,代码看起来像这样:

def func():
    var1 = 'aaa'
    var2 = 'aab'
    t1 = timeit.timeit('var1==var2', 'from __main__ import ___', number = 10**4) #  here I'm missing what to put after the import

If I were trying to execute this code in __main__ I would just import the variable directly with 'from __main__ import var1, var2' Any solution for this kind of issue?如果我试图在__main__中执行此代码,我将直接使用'from __main__ import var1, var2'导入变量 此类问题有什么解决方案吗?

timeit.Timer takes a callable as well as a string to eval timeit.Timer接受一个可调用的以及一个字符串来评估

Changed in version 2.6: The stmt and setup parameters can now also take objects that are callable without arguments.在 2.6 版更改: stmt 和 setup 参数现在也可以采用不带参数可调用的对象。 This will embed calls to them in a timer function that will then be executed by timeit().这会将对它们的调用嵌入到计时器函数中,然后由 timeit() 执行。 Note that the timing overhead is a little larger in this case because of the extra function calls.请注意,在这种情况下,由于额外的函数调用,时间开销会稍大一些。

(also see the source , look for elif hasattr(stmt, '__call__'): ). (另见源代码,寻找elif hasattr(stmt, '__call__'): )。

Create a closure over the variables and pass it to timeit:在变量上创建一个闭包并将其传递给 timeit:

def func():
    var1 = 'aaa'
    var2 = 'aab'
    t1 = timeit.timeit(lambda: var1 == var2, number = 10**4)

or equivalently:或等效地:

def func():
    var1 = 'aaa'
    var2 = 'aab'
    def closure():
        return var1 == var2
    t1 = timeit.timeit(closure, number = 10**4)

The accepted answer didn't work for me inside pdb debugger and a class method.pdb调试器和类方法中,接受的答案对我不起作用。 The solution that worked is to add the variables to globals() :有效的解决方案是将变量添加到globals()

globals()['var1'] = var1
globals()['var2'] = var2
timeit.timeit(lambda: var1 == var2, number = 10**4)

The accepted answer's solution of using lambda creates large overhead.使用lambda的公认答案解决方案会产生大量开销。 Comparison with an alternative, using a setup string:使用设置字符串与替代方案进行比较:

func1 117.3 ms
func2  39.0 ms
func1 116.8 ms
func2  41.6 ms
func1 117.2 ms
func2  35.8 ms

If you're trying to measure such a very fast code snippet, or compare the times of multiple, you'd better not overshadow their execution times with such a function call's large overhead and its variance.如果您正在尝试测量如此快速的代码片段,或比较多个时间,您最好不要用这样的函数调用的大开销及其差异来掩盖它们的执行时间。

Benchmark code that produced the above results ( Try it online! ):产生上述结果的基准代码( 在线试用! ):

import timeit

def func1():
    s1 = 'aaa'
    s2 = 'aab'
    return timeit.timeit(lambda: s1 == s2)

def func2():
    setup = '''
s1 = 'aaa'
s2 = 'aab'
'''
    return timeit.timeit('s1 == s2', setup)

for func in [func1, func2] * 3:
    print(func.__name__,
          '%5.1f ms' % (func() * 1e3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM