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

cProfile.runctx

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


Import

import cProfile
from cProfile import runctx

Signature & Overview

cProfile.runctx(statement, globals, locals, filename=None, sort=-1)

Run statement under profiler, supplying your own globals and locals, optionally saving results in filename.

Usage

import cProfile
import contextlib
import io

buf = io.StringIO()
with contextlib.redirect_stdout(buf):
    cProfile.runctx("sum(range(n))", {}, {"n": 1000}, sort="cumtime")
assert "function calls" in buf.getvalue()

Gotchas

REPL Inspect

from cProfile import runctx
help(runctx)

Linked from

Related notes