O'Reilly logo

Python High Performance Programming by Gabriele Lanaro

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

The dis module

Sometimes, it's not easy to evaluate how many operations a Python statement will take. In this section, we will explore Python internals to estimate the performance of Python statements. Python code gets converted to an intermediate representation—called bytecode—that gets executed by the Python virtual machine.

To help inspect how the code gets converted into bytecode we can use the Python module dis (disassemble). Its usage is really simple, it is sufficient to call the function dis.dis on the ParticleSimulator.evolve method:

import dis
from simul import ParticleSimulator
dis.dis(ParticleSimulator.evolve)

This will generate, for each line, a list of bytecode instructions. For example, the statement v_x = (-p.y)/norm is expanded ...

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