Getting “global name 'foo' is not defined” with Python's timeit
问题 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,