scalene

scalene creates profiles for CPU and memory very quickly. The overhead is usually very low at 10–20%.

Installation

Linux, MacOS and WSL:

$ uv add scalene

Use

  1. An example programme for profiling

[1]:
import numpy as np


def profile_me():
    for _i in range(6):
        x = np.array(range(10**7))
        rng = np.random.default_rng()
        y = np.array(rng.uniform(0, 100, size=(10**8)))
    return x, y
  1. Load scalene

[2]:
%load_ext scalene
Scalene extension successfully loaded. Note: Scalene currently only
supports CPU+GPU profiling inside Jupyter notebooks. For full Scalene
profiling, use the command line version. To profile in line mode, use
`%scrun [options] statement`. To profile in cell mode, use `%%scalene
[options]` followed by your code.

NOTE: in Jupyter notebook on MacOS, Scalene cannot profile child
processes. Do not run to try Scalene with multiprocessing in Jupyter
Notebook.
  1. Profile with only one line of code

[3]:
%scrun profile_me()
import numpy as np


def profile_me():
    for _i in range(6):
        x = np.array(range(10**7))
        rng = np.random.default_rng()
        y = np.array(rng.uniform(0, 100, size=(10**8)))
    return x, y

Scalene: profile saved to scalene-profile.json
  To view in browser:  scalene view
  To view in terminal: scalene view --cli

Create a reduced profile (only rows with non-zero counts)

[4]:
%scrun --reduced-profile profile_me()
import numpy as np


def profile_me():
    for _i in range(6):
        x = np.array(range(10**7))
        rng = np.random.default_rng()
        y = np.array(rng.uniform(0, 100, size=(10**8)))
    return x, y

Scalene: profile saved to scalene-profile.json
  To view in browser:  scalene view
  To view in terminal: scalene view --cli

For a complete list of options, contact:

[5]:
%scrun --help
usage: scalene run [-h] [-o OUTFILE] [--cpu-only] [-c CONFIG_FILE]
                   [--help-advanced]

Profile a Python program with Scalene.

examples:
  % scalene run prog.py                 # profile, save to scalene-profile.json
  % scalene run -o my.json prog.py      # save to custom file
  % scalene run --cpu-only prog.py      # profile CPU only (faster)
  % scalene run -c scalene.yaml prog.py # load options from config file
  % scalene run prog.py --- --arg       # pass args to program
  % scalene run --help-advanced         # show advanced options

options:
  -h, --help            show this help message and exit
  -o, --outfile OUTFILE
                        output file (default: scalene-profile.json)
  --cpu-only            only profile CPU time (no memory/GPU)
  -c, --config CONFIG_FILE
                        load options from YAML config file
  --help-advanced       show advanced options

Profile with more than one line of code in a cell

[ ]:
%%scalene --reduced-profile
x = 0
for _i in range(1000):
    for _j in range(1000):
        x += 1