Chris Myers, Andrew Dolgert (original author)
Cornell Center for Advanced Computing

Revisions: 6/2023, 5/2020, 8/2018, 6/2015, 5/2011 (original)

As noted in a previous section on algorithms and performance, it is important to take an empirical perspective on run time optimization through the use of various performance assessment tools. Support for code profiling and timing is available within the Python and IPython interpreters; we've already seen several uses of the IPython %timeit function. Profiling provides information on which functions are called, the number of times they are called, and the cumulative and per-call run time associated with each function. Profiling is thus especially useful in identifying code "hotspots" that account for a large fraction of the overall program run time. Timing (or benchmarking) involves characterizing how long an entire program — or specific functions within that program — take to execute, perhaps as a function of the size of the relevant data being operated upon. Profiling can be used to get an overall picture of where one should best focus one's efforts on performance optimization, and then more targeted timing tests can be used to evaluate the efficacy of different optimization strategies.

Objectives

After completing this topic, you should be able to:

  • Discuss how to cultivate an empirical perspective on run time optimization
  • Use profiling tools to find hotspots in Python programs
  • Use timing tools to assess the run time of different snippets or collections of Python code
Prerequisites

As this topic focuses on accelerating Python programs for scientific computing, it implicitly assumes the reader has some prior experience programming in Python, as well as working knowledge of general programming concepts. The target audience is scientists and engineers who are already programming in Python, and are interested in using Python tools and packages to improve the run time performance of the programs they are developing. If additional introductory material about Python is needed, readers can consult Introduction to Python Programming as well as the documentation on the python.org website.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement