Python timeit NameError

时间:2016-01-26 18:37:24

标签: python import nameerror timeit

我想在Python 3.5中使用timeit来测量两个函数。第一个依赖import math,第二个依赖from math import log2。我虽然可以通过在调用timeit.repeat时将适当的import语句作为设置字符串传递来处理此问题。但我得到NameError: name 'math' is not defined。 我不想将这些函数作为其名称字符串传递。当函数不依赖于导入时,这里的代码可行,但我需要调用math.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, '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))

1 个答案:

答案 0 :(得分:4)

timeit.repeat运行的

The inner function如下所示:

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

其中{setup}被设置字符串替换,{stmt}被语句字符串替换。请注意,设置是从inner内部运行的。因此,如果在setup中放置import语句,则模块名称将成为inner本地变量。稍后,当从spam_log_v1调用stmt时,模块名称不可用于spam_log_v1,因为模块名称不在全局范围内。

该修复只是在脚本的全局级别导入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))