I'm just playing around in python with timeit
and the following code works properly:
def mysleep(n):
import time
time.sleep(n)
import timeit
for k in range (1,5):
def mytime():
mysleep(k)
t1 = timeit.Timer("mytime();", "from __main__ import mytime")
print k, t1.timeit(1)
But if I put the same code inside a function, for each k
the time is about 3 seconds.
def mytest():
import timeit
for k in range (1,5):
def mytime():
mysleep(k)
t1 = timeit.Timer("mytime();", "from __main__ import mytime")
print k, t1.timeit(1)
mytest()
Why doesn't my code inside a function work and how can I fix it?