cProfile
is a C
extension introduced in Python 2.5. It can be used for deterministic profiling. Deterministic profiling means that the time measurements obtained are precise and no sampling is used. This contrasts with statistical profiling, where measurements come from random samples. We will profile a small NumPy program using cProfile
, which transposes an array with random values.
Again, we require code to profile:
Write the following
transpose()
function to create an array with random values and transpose it:def transpose(n): random_values = np.random.random((n, n)) return random_values.T
Run the profiler and give it the function to profile:
cProfile.run('transpose (1000)')
The complete code for this tutorial can be found in the following snippet:
import numpy as np import cProfile def transpose(n): random_values = np.random.random((n, n)) return random_values.T cProfile.run('transpose (1000)')
For a 1000 x 1000 array, we...