Now that we've installed line_profiler
, we can start profiling.
Obviously, we will need code to profile:
Write the following code to multiply a random matrix of varying size by itself. Also, the thread will sleep for a few seconds. Annotate the function to profile with
@profile
:import numpy as np import time @profile def multiply(n): A = np.random.rand(n, n) time.sleep(np.random.randint(0, 2)) return np.matrix(A) ** 2 for n in 2 ** np.arange(0, 10): multiply(n)
Run the profiler with the following command:
$ kernprof.py -l -v mat_mult.py Wrote profile results to mat_mult.py.lprof Timer unit: 1e-06 s File: mat_mult.py Function: multiply at line 4 Total time: 3.19654 s Line # Hits Time Per Hit % Time Line Contents ============================================================== 4 @profile 5 def multiply(n): 6 10 ...