I understand the concept of what timeit
does but I am not sure how to implement it in my code.
How can I compare two functions, say insertion_sort
and tim_sort
, with timeit
?
I understand the concept of what timeit
does but I am not sure how to implement it in my code.
How can I compare two functions, say insertion_sort
and tim_sort
, with timeit
?
I find the easiest way to use timeit is from the command line:
Given test.py:
run timeit like this:
The built-in timeit module works best from the IPython command line.
To time functions from within a module:
for me, this is the fastest way:
This works great:
You would create two functions and then run something similar to this. Notice, you want to choose the same number of execution/run to compare apple to apple.
This was tested under Python 3.7.
Here is the code for ease of copying it