获取“全局名称‘foo’” 未定义” 使用Python的timeit

发布于 2024-07-13 21:01:46 字数 792 浏览 7 评论 0原文

我想知道执行一条Python语句需要多少时间,所以我上网查了一下,发现标准库提供了一个名为 timeit 声称正是这样做的:

import timeit

def foo():
    # ... contains code I want to time ...

def dotime():
    t = timeit.Timer("foo()")
    time = t.timeit(1)
    print "took %fs\n" % (time,)

dotime()

但是,这会产生一个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in dotime
  File "/usr/local/lib/python2.6/timeit.py", line 193, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
NameError: global name 'foo' is not defined

我对 Python 还是新手,我不完全理解它所具有的所有范围问题,但是我不知道为什么这个片段不起作用。 有什么想法吗?

I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called timeit that purports to do exactly that:

import timeit

def foo():
    # ... contains code I want to time ...

def dotime():
    t = timeit.Timer("foo()")
    time = t.timeit(1)
    print "took %fs\n" % (time,)

dotime()

However, this produces an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in dotime
  File "/usr/local/lib/python2.6/timeit.py", line 193, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
NameError: global name 'foo' is not defined

I'm still new to Python and I don't fully understand all the scoping issues it has, but I don't know why this snippet doesn't work. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不疑不惑不回忆 2024-07-20 21:01:46

更改此行:

t = timeit.Timer("foo()")

为此:

t = timeit.Timer("foo()", "from __main__ import foo")

查看您在最底部提供的链接。

要让 timeit 模块访问您定义的函数,您可以传递一个包含 import 语句的设置参数:

我刚刚在我的机器上测试了它,并且它可以处理更改。

Change this line:

t = timeit.Timer("foo()")

To this:

t = timeit.Timer("foo()", "from __main__ import foo")

Check out the link you provided at the very bottom.

To give the timeit module access to functions you define, you can pass a setup parameter which contains an import statement:

I just tested it on my machine and it worked with the changes.

可爱暴击 2024-07-20 21:01:46

globals=globals()

t = timeit.Timer("foo()", globals=globals())

对于 Python 3,您可以使用

另一个选项是将 globals() 传递给 globals 参数,该参数
将导致代码在您当前的全局中执行
命名空间。 这比单独指定更方便
进口

With Python 3, you can use globals=globals()

t = timeit.Timer("foo()", globals=globals())

From the documentation:

Another option is to pass globals() to the globals parameter, which
will cause the code to be executed within your current global
namespace. This can be more convenient than individually specifying
imports

云雾 2024-07-20 21:01:46

你可以尝试这个技巧:

import timeit

def foo():
    print 'bar'

def dotime():
    t = timeit.Timer("foo()")
    time = t.timeit(1)
    print "took %fs\n" % (time,)

import __builtin__
__builtin__.__dict__.update(locals())

dotime()

You can try this hack:

import timeit

def foo():
    print 'bar'

def dotime():
    t = timeit.Timer("foo()")
    time = t.timeit(1)
    print "took %fs\n" % (time,)

import __builtin__
__builtin__.__dict__.update(locals())

dotime()
薔薇婲 2024-07-20 21:01:46
t = timeit.Timer("foo()", "from __main__ import foo")

因为 timeit 的范围内没有你的东西。

t = timeit.Timer("foo()", "from __main__ import foo")

Since timeit doesn't have your stuff in scope.

厌味 2024-07-20 21:01:46

添加到您的设置中“import thisfile;”

然后当您调用设置函数 myfunc() 时使用“thisfile.myfunc()”

例如“thisfile.py”

def myfunc():

 return 5

def testable(par):

 pass



t=timeit.timeit(stmt="testable(v)",setup="import thisfile; v=thisfile.myfunc();").repeat(10)

print( t )

add into your setup "import thisfile; "

then when you call the setup function myfunc() use "thisfile.myfunc()"

eg "thisfile.py"

def myfunc():

 return 5

def testable(par):

 pass



t=timeit.timeit(stmt="testable(v)",setup="import thisfile; v=thisfile.myfunc();").repeat(10)

print( t )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文