Python profiling libraries

Updated Nov 2026

Python profiling libraries measure where a program spends its time or memory, down to the function or line. A sampling profiler records the call stack at intervals rather than timing every function call. These libraries let developers find the code that slows down a service or exhausts its memory.

Popularity

py-spy has the most monthly downloads in this comparison, at 21.3M.

LibraryMonthly downloadsStarsLast releaseLicense
py-spy
21.3M
15.7KNov 2026MIT
Memray
10.9M
15.4KAug 2026Apache-2.0
Pyinstrument
9.5M
8.0KJul 2026BSD-3-Clause
memory_profiler
5.4M
4.6KNov 2022BSD
Yappi
3.0M
1.7KMar 2026MIT
line_profiler
2.1M
3.3KOct 2026BSD-3-Clause
VizTracer
1.5M
7.8KNov 2025Apache-2.0
Scalene
293.0K
13.6KOct 2026Apache-2.0
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Features compared

LibraryPython APIExecution timingMemory profiling
py-spyUnsupportedBuilt-inUnsupported
MemrayBuilt-inUnsupportedBuilt-in
PyinstrumentBuilt-inBuilt-inUnsupported
memory_profilerBuilt-inUnsupportedBuilt-in
YappiBuilt-inBuilt-inUnsupported
line_profilerBuilt-inBuilt-inUnsupported
VizTracerBuilt-inBuilt-inUnsupported
ScaleneBuilt-inBuilt-inBuilt-in
Built-inNot supported

Libraries and code examples

Samples Python programs to profile them.

Downloads / mo
21.3M
Stars
15.7K
Last release
0.4.3
License
MIT

Quickstart

From README.md
$ python -m pip install py-spy==0.4.3
py-spy record -o profile.svg -- python your_script.py

py-spy examples

From open-source projects on GitHub

Profiles memory in Python applications.

Downloads / mo
10.9M
Stars
15.4K
Last release
1.20.0
License
Apache-2.0

Quickstart

From api.rst
$ python -m pip install memray==1.20.0
import memray

with memray.Tracker("profile.bin"):
    values = [i * i for i in range(10000)]

Memray examples

From open-source projects on GitHub

Samples Python call stacks to explain elapsed time across nested application work.

Downloads / mo
9.5M
Stars
8.0K
Last release
5.1.3
License
BSD-3-Clause

Strengths

  • Shows the full call stack. Trace elapsed time through callers and nested function work.
  • Renders browser-readable reports. Export HTML or JSON reports through its renderer interface.

Limitations

  • Elapsed time includes I/O waits. Waiting for network, files, or databases contributes to reported duration.
  • Sampling omits exact call counts. Sampled stacks describe elapsed activity rather than every function invocation.

Quickstart

From guide.html
$ python -m pip install pyinstrument==5.1.3
from pyinstrument import Profiler
import time

profiler = Profiler()
profiler.start()
time.sleep(0.05)
profiler.stop()
print(profiler.output_text())

Pyinstrument examples

From open-source projects on GitHub

memory_profiler

Monitors the memory usage of a Python program.

Downloads / mo
5.4M
Stars
4.6K
Last release
0.61.0
License
BSD

Quickstart

From memory_profiler.py
$ python -m pip install memory-profiler==0.61.0
from memory_profiler import memory_usage

print(memory_usage(-1, interval=0.1, timeout=0.2))

memory_profiler examples

From open-source projects on GitHub

Traces Python function calls with CPU or wall clocks and thread-aware profiling statistics.

Downloads / mo
3.0M
Stars
1.7K
Last release
1.7.6
License
MIT

Strengths

  • Chooses CPU or wall time. Switch clocks to separate CPU consumption from elapsed waiting.
  • Separates work by thread. Filter function statistics by thread context identifiers.

Limitations

  • CPU timing omits blocked waits. Choose wall time when blocked I/O belongs in the measurement.
  • Results persist until explicitly cleared. Call yappi.clear_stats between independent profiling sessions.

Quickstart

From README.md
$ python -m pip install yappi==1.7.6
import yappi

def work():
    return sum(i * i for i in range(10000))

yappi.start()
work()
yappi.stop()
yappi.get_func_stats().print_all()

Yappi examples

From open-source projects on GitHub

Profiles code line by line.

Downloads / mo
2.1M
Stars
3.3K
Last release
5.1.0
License
BSD-3-Clause

Quickstart

From line_profiler.py
$ python -m pip install line-profiler==5.1.0
from line_profiler import LineProfiler

def total():
    return sum(range(10000))

profiler = LineProfiler()
profiler.add_function(total)
profiler.enable()
total()
profiler.disable()
profiler.print_stats()

line_profiler examples

From open-source projects on GitHub

Traces and visualizes Python code execution.

Downloads / mo
1.5M
Stars
7.8K
Last release
1.1.1
License
Apache-2.0

Quickstart

From README.md
$ python -m pip install viztracer==1.1.1
from viztracer import VizTracer

def work():
    return sum(range(10000))

with VizTracer(output_file="trace.json"):
    work()

VizTracer examples

From open-source projects on GitHub

Profiles source-line CPU activity and memory allocation, with optional NVIDIA GPU reporting.

Downloads / mo
293.0K
Stars
13.6K
Last release
2.4.0
License
Apache-2.0

Strengths

  • Attributes cost to source lines. Find Python lines responsible for CPU activity and memory growth.
  • Separates Python and native work. Distinguish Python execution from time spent in native extension code.

Limitations

  • GPU timing needs NVIDIA hardware. GPU-time reporting is currently limited to NVIDIA-based systems.
  • Memory tests require a relaunch. Pytest memory mode restarts the session to initialize allocation tracking.

Quickstart

From README.md
$ python -m pip install scalene==2.4.0
scalene run your_script.py

FAQ

py-spy suits profiling a running process without changing its code. Pyinstrument suits readable call-stack reports from inside the program. Memray suits tracking down memory allocations.

Scalene samples CPU time, memory, and GPU activity and attributes them to Python and native code.

py-spy is a sampling profiler that attaches to a running Python process from outside it. It needs no changes to the application and may require extra permissions to attach.

Memray is a memory profiler that tracks allocations from Python, the interpreter, and native code, with the stack that made each one.

Memray records each allocation and the code that made it. memory_profiler samples memory usage line by line or for a whole process.

For coding agents

Found your profiling library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official