Python: is there a way to import a variable using timeit.timeit()?

ぐ巨炮叔叔 提交于 2019-11-30 18:24:42

Import x from __main__ as well:

timeit.timeit("function(x)", setup="from __main__ import function, x")

Just like function, x is a name in the __main__ module, and can be imported into the timeit setup.

You can avoid this problem entirely if you pass timeit a function instead of a string. In that case, the function executes in its normal globals and closure environment. So:

timeit.timeit(lambda: function(x))

Or, if you prefer:

timeit.timeit(partial(function, x))

(See here for details. Note that it requires Python 2.6+, so if you need 2.3-2.5, you can't use this trick.)


As the documentation says, "Note that the timing overhead is a little larger in this case because of the extra function calls."

This means that it makes timeit itself run slower. For example:

>>> def f(): pass
>>> timeit.timeit('timeit.timeit("f()", setup="from __main__ import f")', setup='import timeit', number=1000)
91.66315175301861
>>> timeit.timeit(lambda: timeit.timeit(f), number=100)
94.89793294097762

However, it doesn't affect the actual results:

>>> timeit.timeit(f, number=100000000)
8.81197881908156
>>> timeit.timeit('f()', setup='from __main__ import f', number=100000000)
8.893913001054898

(In the rare cases where it does, that typically means one version or the other wasn't testing the function the way it will be called in your real code, or was testing the wrong closure or similar.)

Note that the actual time taken inside the function here is about 88 seconds, so we've nearly doubled the overhead of timing code… but it's still only added 3% to the total testing time. And the less trivial f is, the smaller this difference will be.

Import x from __main__:

timeit.timeit("function(x)",setup="from __main__ import function, x")

Alternatively, you can add x to globals. The good thing about it is that it works in a pdb debugging session:

globals()['x'] = x
timeit.timeit(lambda: function(x))

Note that the timing overhead is a little larger in this case because of the extra function calls. [source]

With Python 3.5, the optional argument globals has been introduced. It makes it possible to specify the namespace in which the timeit statement shall be executed.

So instead of writing:

timeit.timeit("function(x), setup="from __main__ import function, x")

...you can now write:

timeit.timeit("function(x)", globals=globals())

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!