Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Profiling a program with IPython


As most of us learned in programming classes, premature optimization is the root of all evil. However, once you approach the final stages of software development, it could very well be that certain parts of the code are unnecessarily slow or use more memory than is strictly needed. We can find these issues through the process of profiling. Profiling involves measuring metrics such as execution time for a piece of code such as a function or a single statement.

IPython is an interactive Python environment, which also comes with a shell similar to the standard Python shell. In IPython, we can profile small snippets of code using timeit. We can also profile a larger script. We will show both approaches.

  1. Timing a snippet:

    Start IPython in pylab mode

    ipython -pylab
    
  2. Create an array containing 1,000 integer values between 0 and 1,000.

    In [1]: a = arange(1000)
    

    This is the time to search for the answer to everything 42 in the array.

    In [2]: %timeit searchsorted(a, 42...