On this page
  1. cProfile.run
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

cProfile.run

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


Import

import cProfile
from cProfile import run

Signature & Overview

cProfile.run(statement, filename=None, sort=-1)

Run statement under profiler optionally saving results in filename This function takes a single argument that can be passed to the "exec" statement, and an optional file name. In all cases this

Usage

import cProfile
import contextlib
import io

buf = io.StringIO()
with contextlib.redirect_stdout(buf):
    cProfile.run("sum(range(1000))", sort="tottime")
assert "function calls" in buf.getvalue()

Gotchas

REPL Inspect

from cProfile import run
help(run)

Linked from

Related notes