0

I read that the time unit returned by the timeit module is seconds.

However, if I have multiple repetitions, e.g.

min(timeit.Timer('my_function(t)', 
                      'from __main__ import my_function, t')
                              .repeat(repeat=50, number=1000)))

would I have to divide the result by 1000 to get the actual seconds per loop, or does it already account for that?

1 Answers1

4

Yes, it is the total time which is returned. You have to divide by 1000 if you really want to have the time per iteration of the loop. A simple test could have showed you that easily:

>>> timeit.timeit("import time; time.sleep(1)", number=1)
1.0000581741333008
>>> timeit.timeit("import time; time.sleep(1)", number=5)
5.000285863876343
>>> timeit.repeat("import time; time.sleep(1)", number=5, repeat=3)
[5.000285863876343, 5.003287076950073, 5.000286102294922]
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97