What Is Your Python Budget?

C programmers, by necessity, generally develop a mental model for understanding the performance characteristics of their code. Developing this intuition in a high level language like Python can be more of a challenge. While good Python tools exist for identifying time and memory performance (line_profiler by Robert Kern and guppy by Sverker Nilsson), you are largely on your own if you want to develop intuition for code that is yet to be written. Understanding the cost of basic operations in your Python implementation can help guide design decisions by ruling out extensive use of expensive operations.

Why is this important you ask? Interactive applications appear responsive when they react to user behaviour within a given time budget. In our consulting engagements, we often find that a lack of awareness regarding the cost of common operations can lead to sluggish application performance. Some examples of user interaction thresholds:

  • If you are targeting 60 fps in a multimedia application you have 16 milliseconds of processing time per frame. In this time, you need to update state, figure out what is visible, and then draw it.
  • Well behaved applications will load a functional screen that a user can interact with in under a second. Depending on your application, you may need to create an expensive datastructure upfront before your user can interact with the application. Often one needs to find a way to at least make it feel like the one second constraint is being respected.
  • You run Gentoo / Arch. In this case, obsessing over performance is a way of life.

Obviously rules are meant to be broken, but knowing where to be frugal can help you avoid or troubleshoot performance problems. Performance data for Python and PyPy are listed below.

Machine configuration

CPU – AMD 8150

RAM – 16 GB PC3-12800

Windows 7 64 bit

Python 2.7.2 — EPD 7.3-1 (64-bit)

PyPy 2.0.0-beta1 with MSC v.1500 32 bit
Steps to obtain timings and create table from data

python measure.py cpython.data
pypy measure.py pypy.data

python draw_table.py cpython.data cpython.png
python draw_table.py pypy.data pypy.png

To obtain the code to measure timings and create the associated tables for your own machine, checkout https://github.com/deepankarsharma/cost-of-python

CPython timing data


PyPy timing data



7 thoughts on “What Is Your Python Budget?

  1. avatarGaëtan de Menten

    Your second (data = …) and fourth (alist = …) examples on the image are the same. I guess you meant to time the difference between tuple indexing and list indexing but this is not the case…

  2. avatarDeepankar Sharma

    Thanks for noticing the error Gaetan, I have corrected it. I have changed data to access an entry from a tuple. I have also added an entry for sorting a medium sized list. Thanks for taking the time to read the post.

  3. avatarCarl Friedrich Bolz

    For PyPy some of the measurements are not all that helpful. For those where something is allocated the JIT figures out that the object is not actually needed and removes the actual allocation. The fastest few benchmarks are therefore probably mostly measuring the speed of the timing function.

  4. Pingback: PyPy应该在什么场景下使用,以及应该注意什么 - python - 开发者问答

Leave a Reply

Your email address will not be published. Required fields are marked *

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 106,455 bad guys.