On this page
  1. cProfile.Profile
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

cProfile.Profile

Up: cProfile | Next: cProfile.run
Rel: cProfile.run, cProfile.runctx
Ref: docs.python.org — cProfile.Profile
Tags: #public #python3 #stdlib #cProfile


Import

import cProfile
from cProfile import Profile

Signature & Overview

cProfile.Profile(timer=None, timeunit=0.0, subcalls=True, builtins=True)

Builds a profiler object using the specified timer function.

Methods & Attributes

Usage

import cProfile
import io
import pstats

with cProfile.Profile() as pr:
    _ = sum(i * i for i in range(2000))

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

Gotchas

REPL Inspect

from cProfile import Profile
help(Profile)

Linked from

Related notes