The timeit module can be used in an interactive Python session, there are two convenient options:
The first option is to use the IPython shell. It features the convenient %timeit special function.
def f(x):
return x*x
%timeit for x in range(100): f(x)
The second option is to use it in a standard Python interpreter, where you can access functions and other names you defined earlier during the interactive session by importing them from __main__ in the setup statement:
def f(x):
return x * x
import timeit
timeit.repeat("for x in range(100): f(x)", "from __main__ import f", number=100000)
To know more about this you can have a look at the following video tutorial:-