Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Profiling

If you want to make code faster, always profile first; you want to spend your time on the slow parts, and you only want to have the important bits of your code designed to go fast - everything else should be designed to be clean and maintainable.

Line profiler

There is a build in cProfile (one of the very, very few modules to still have the “classic” Python 2 naming scheme; new name coming in Python 3.15), but instead, let’s use line_profiler and its IPython magic. Let’s start with this simple file that contains a MC sampler:

Writing temp_prof.py

Memory profiling

Please see Fil. It doesn’t provide Python 3.14 wheels yet, and doesn’t work with notebooks, so not included here.

Modern sampling profiling

Python 3.15 will have a built-in sampling profiler, and it’s great. IMO, grabbing an alpha release (with uv) is already easier than setting up any other sampling profiler.