On this page
  1. profile
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Calibrating Pure-Python Overhead
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects

profile

Prev: pprint | Up: stdlib_module | Next: pstats
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — profile
Tags: #public #python3 #stdlib


Import

import profile

Overview

Pure-Python deterministic function profiler (profile.Profile, profile.run()), sharing the same API and .prof output format as the C-accelerated cProfile module. Reach for cProfile for everyday profiling; use profile only when you need to subclass Profile in Python or run in an environment where C extensions are unavailable.

Quick Start

import io
import profile
import pstats

pr = profile.Profile()
pr.enable()
total = sum(i * i for i in range(1000))
pr.disable()

buf = io.StringIO()
pstats.Stats(pr, stream=buf).sort_stats("tottime").print_stats(5)
assert "function calls" in buf.getvalue()

Implementation Walkthrough

1. Calibrating Pure-Python Overhead

Because profile is written in Python, it adds noticeable overhead to every function call. You can measure and set bias on a Profile instance using pr.calibrate(10000) to compensate for interpreter dispatch cost before recording timings.

Gotchas & Boundaries

Common Functions

Classes & Objects

Linked from

Related notes