简体   繁体   English

Python timeit NameError

[英]Python timeit NameError

I want to use timeit in Python 3.5 to measure two functions. 我想在Python 3.5中使用timeit来测量两个函数。 The first one relies on import math and the second one on from math import log2 . 第一个依赖于import math ,第二个依赖from math import log2 I though I can handle this by passing the appropiate import statement as setup string when calling timeit.repeat . 我虽然可以通过在调用timeit.repeat时将适当的import语句作为设置字符串传递来处理这个timeit.repeat But I get NameError: name 'math' is not defined . 但我得到NameError: name 'math' is not defined I don't want to pass the functions as their name strings. 我不想将这些函数作为其名称字符串传递。 When the functions don't depend on imports, this code here works, but I need them with the calls of math.log2 当函数不依赖于导入时,这里的代码可行,但我需要调用math.log2

Looking foward to your answers. 期待你的答案。

def spam_log_v1():
    for _ in range(1000):
        math.log2(2)


def spam_log_v2():
    for _ in range(1000):
        log2(2)


if __name__ == '__main__':

    import timeit

    repeat = 3
    number = 1000
    unit = "usec"
    unittosec = {"usec": 1e6, "msec": 1000, "sec": 1}
    tests = [(spam_log_v1, 'import math'),
             (spam_log_v2, 'from math import log2')]

    for fct, setup in tests:
        res = timeit.repeat(fct, setup=setup, repeat=repeat, number=number)
        print("%s: %d loops, best of %d: %.3g %s per loop" %
              (fct.__name__, number, repeat,
               min(res) / number * unittosec[unit], unit))

The inner function which is run by timeit.repeat looks like this: timeit.repeat运行inner函数如下所示:

def inner(_it, _timer{init}):
    {setup}
    _t0 = _timer()
    for _i in _it:
        {stmt}
    _t1 = _timer()
    return _t1 - _t0

where {setup} is replaced by the setup string and {stmt} is replaced by the statement string. 其中{setup}被设置字符串替换, {stmt}被语句字符串替换。 Notice that the setup is run from inside inner . 请注意,该设置是从内部运行inner So if you place an import statement in setup , the module name becomes a local variable of inner . 因此,如果在setup放置import语句,模块名称将成为inner局部变量 Later, when spam_log_v1 is called from the stmt , the module name is not available to spam_log_v1 since the module name is not in the global scope. 稍后,当从stmt调用spam_log_v1时,模块名称不可用于spam_log_v1因为模块名称不在全局范围内。

The fix is to simply import math and/or from math import log2 at the global level of your script. 修复方法是在脚本的全局级别简单地导入math和/或from math import log2

import math
from math import log2

def spam_log_v1():
    for _ in range(1000):
        math.log2(2)

def spam_log_v2():
    for _ in range(1000):
        log2(2)

if __name__ == '__main__':
    import timeit
    repeat = 3
    number = 1000
    unit = "usec"
    unittosec = {"usec": 1e6, "msec": 1000, "sec": 1}
    tests = [
        (spam_log_v1, ''),
        (spam_log_v2, '')]

    for fct, setup in tests:
        res = timeit.repeat(fct, setup=setup, repeat=repeat, number=number)
        print("%s: %d loops, best of %d: %.3g %s per loop" %
              (fct.__name__, number, repeat,
               min(res) / number * unittosec[unit], unit))

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

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