Python Profiling Tools Compared: cProfile, py-spy, Scalene

How to profile slow Python code: cProfile, py-spy, line_profiler, Scalene, tracemalloc and Python 3.15’s sampling profiler, and which to use when.

Start with cProfile because it is built in; reach for py-spy when you cannot change the code, and line_profiler or Scalene when you need to see inside a function. Always profile before optimising, because the slow part of a program is rarely where you expect.

At a glance

Tool Type Best for Needs code change
cProfile Deterministic, built in Which functions are slow No
py-spy Sampling, external Live or production processes No
line_profiler Line-by-line Hot spots inside one function Decorator
Scalene Sampling CPU, memory and more by line No
tracemalloc Memory, built in Allocation snapshots and leaks Small snippet
profiling.sampling Sampling, built in Low-overhead profiling on 3.15 No

cProfile

python -m cProfile -o out.prof script.py
python -c "import pstats; pstats.Stats('out.prof').sort_stats('cumulative').print_stats(15)"

Sorting by cumulative time shows the functions that account for the most total time, including what they call. The overhead is noticeable since it traces every call, but the output is exact about call counts. A viewer such as SnakeViz turns the profile file into an interactive chart.

py-spy

py-spy samples a running Python process from outside, so you can attach it to something that is already slow in production:

py-spy top --pid 12345
py-spy record -o profile.svg --pid 12345

Because it samples, overhead is low and you don’t need to restart or modify the program. py-spy 0.4.3 (2 November 2026) added Python 3.15 support and 0.4.4 (19 November) added free-threaded interpreters, according to its release notes. Depending on your operating system and settings, attaching to a process may need elevated privileges (on macOS usually sudo); py-spy’s README has a section on when you need it.

line_profiler

Once you know which function is slow, line_profiler shows time per line:

@profile
def process(rows):
    ...

Run it with kernprof -l -v script.py. It adds overhead and requires marking the function, so use it after cProfile has narrowed the search.

Scalene

Scalene is a sampling profiler that reports CPU time, memory use and more per line, in a single run. It is useful when you are not sure whether the problem is CPU or memory. In Scalene 2.4.0 you profile with scalene run script.py, which saves scalene-profile.json, then look at it with scalene view (in the browser) or scalene view --cli (in the terminal). It ran on Python 3.15.2 and wrote per-line CPU and memory fields.

Memory with tracemalloc

import tracemalloc

tracemalloc.start()
data = [bytes(1000) for _ in range(2000)]  # stand-in for the suspect code
snapshot = tracemalloc.take_snapshot()
for stat in snapshot.statistics("lineno")[:10]:
    print(stat)  # roughly: <file>:5: size=2033 KiB, count=2001, average=1041 B

Take snapshots before and after an operation and compare them to find what is growing.

Python 3.15’s built-in sampling profiler

Python 3.15 organises its tools under a new profiling package: profiling.tracing (the deterministic profiler behind cProfile) and profiling.sampling, a statistical sampler named Tachyon. cProfile keeps working. The documentation says Tachyon can run a script directly or attach to a running process without code changes, and that because sampling happens outside the target process its overhead is virtually zero.

python -m profiling.sampling run script.py        # run and profile a script
python -m profiling.sampling attach 1234          # attach to a running process
python -m profiling.sampling run --flamegraph script.py
python -m profiling.sampling run --live script.py # interactive, top-like view

The default output is pstats; other options include --flamegraph, --heatmap, --collapsed and --gecko (for the Firefox Profiler), and --mode chooses wall-clock, CPU, GIL or exception sampling. It needs permission to read process memory, and that bit people. On macOS the documentation says it needs root (or a debugger entitlement), and running python -m profiling.sampling run script.py as an ordinary user on macOS stopped with “Unable to access process memory on macOS” and a suggestion to use sudo -E. On Windows it needs administrator rights, and on Linux attaching to a process you did not start is limited by ptrace_scope unless you have CAP_SYS_PTRACE. The profiler and the target process must also be the same Python minor version. See what is new in Python 3.15.

Checked on 25 November 2026 with Python 3.15.2 on macOS: the cProfile, pstats and tracemalloc examples, kernprof -l -v with line_profiler 5.1.0, and scalene run with Scalene 2.4.0 all ran. The profiling.sampling options come from its --help output and the Python 3.15 documentation, but the sampler itself would not start without elevated privileges, so its output was not captured. py-spy 0.4.4’s options were checked from its help output, and attaching needs elevated privileges on macOS too.

A practical workflow

  1. Reproduce the slowness with a realistic input.
  2. Run cProfile (or py-spy in production) and find the top functions.
  3. Use line_profiler on the top one.
  4. Change one thing, re-measure, and keep the change only if it helps.

Before optimising code, check whether the work could run concurrently; see asyncio vs threading vs multiprocessing and free threading explained. For a faster interpreter, see the Python 3.15 JIT, and for data-heavy code, Polars vs pandas.

Frequently Asked Questions

What is the best Python profiler?

cProfile is the best starting point because it is built in and shows which functions take the most time. Use py-spy to profile a running process without changing code, line_profiler to see time per line, and Scalene or tracemalloc for memory.

How do I profile a Python script?

Run python -m cProfile -o out.prof script.py, then inspect it with the pstats module or a viewer such as SnakeViz. Sort by cumulative time to find the functions that dominate the run.

What is a sampling profiler?

A sampling profiler periodically records what the program is doing rather than tracing every call, so it adds less overhead. py-spy is a popular external one, and Python 3.15 adds a built-in sampling profiler called Tachyon in the new profiling package. Like py-spy, it reads another process’s memory, so on macOS and Windows it needs elevated privileges.

How do I find a memory leak in Python?

Use tracemalloc from the standard library to take snapshots and compare them, or a tool such as Scalene to see memory by line. Compare snapshots taken before and after the suspect operation.

Should I optimise before profiling?

No. Measure first. The slow part of a program is usually not where you expect it, and profiling stops you spending time on code that does not matter.