O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Profiling code with the cProfile extension

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.

How to do it...

Again, we require code to profile:

  1. 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
  2. Run the profiler and give it the function to profile:
    cProfile.run('transpose (1000)')

    The complete ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required