10.2 Timing how long your code takes to run
If I want to improve my code to run faster, I must measure how long it takes to run before and after my changes. In section 6.12.4, I covered computing execution time with the time module. Now let’s look at timeit.
The timeit function takes a string containing a Python expression and evaluates it many times. It returns the total time for the entire execution.
import timeit
timeit.timeit("2**1000")
0.8334714000000001
In this example, Python evaluated 2**1000
one million times and returned the
number of seconds for the entire computation. You can use keyword arguments to specify the code
and the number of iterations.
timeit.timeit(stmt="2**1000", number=500000)
0.4071590999999999
Exercise 10.1
In the graph of timeit times for 2n with 60 ≤ n ≤ 65 in Figure...