I was quickly trying to time 2 functions in ipython, m1()
and m2()
doing the same task with 2 different implementation.
In [23]: %timeit for x in range(100): m1(a)
10000 loops, best of 3: 57.6 us per loop
In [24]: %timeit for x in range(100): m2(a)
10000 loops, best of 3: 108 us per loop
Result: the first implementation is almost 2x faster. So far, so good.
Out of curiousity, I changed the range of the for
loop above, and now I am at a loss making sense of the output.
In [25]: %timeit for x in range(1): m2(a)
1000000 loops, best of 3: 1.29 us per loop
In [26]: %timeit for x in range(10): m2(a)
100000 loops, best of 3: 10.8 us per loop
In [27]: %timeit for x in range(1000): m2(a)
1000 loops, best of 3: 1.06 ms per loop
What exactly is the for loop doing here? And why do the value of the number of loops decrease on increasing the range value?
PS: I was using this as the reference. Also, please edit the title to something better if it doesn't exactly convey my question.