I have a very simple test function that I need to capture its execution time using 'timeit' module, but I get an error
The function:
import timeit
def test1():
l = []
for i in range(1000):
l = l + [i]
t1 = timeit.Timer("test1()", "from __main__ import test1")
print(t1.timeit(number=1000))
The Error: C:\Python34\lib\timeit.py:186: in timeit timing = self.inner(it, self.timer) :3: in inner ??? E
ImportError: cannot import name 'test1' =========== 1 error in 0.03 seconds ==============
Can you guys help me with a solution?
test1
. You need to indent lines 3,4,5. I suspect the problem is thattimeit
is manually importing your code, and getting an import error. – Martin Bonner supports Monica Sep 2 '16 at 11:22