# pydoc > profile

---
type: CommandReference
command: profile
mode: pydoc
section: ''
source: pydoc3
---

## Quick Reference
- `profile.run('statement')` — Profile a single statement and print report.
- `profile.runctx('statement', globals, locals)` — Profile with custom globals/locals.
- `pr = profile.Profile(); pr.run('code')` — Create a profiler instance and run code.
- `pr.runcall(func, *args, **kw)` — Profile a function call.
- `pr.print_stats(sort='time')` — Print profiling stats sorted by time.
- `pr.dump_stats('output.prof')` — Save stats to a file.
- `pr.calibrate(100)` — Calibrate profiler timer.

## Name
Class for profiling Python code.

## Synopsis
python
import profile

# Quick profiling
profile.run('code')

# Profiling with custom environment
profile.runctx('code', globals(), locals())

# Using the Profile class
pr = profile.Profile()
pr.run('code')
pr.print_stats()
## Options

### `Profile` class
`Profile(timer=None, bias=None)` — Profiler class.

Internal data structures:
- `self.cur` — tuple representing the active stack frame.  
  Elements: `[0]` time to charge to parent, `[1]` total time in this function (excluding subfunctions), `[2]` total time in subfunctions, `[-3]` function name, `[-2]` frame, `[-1]` parent tuple.
- `self.timings` — dict of function timing data. Each entry is a 5‑tuple:  
  `[0]` call count (excluding recursion), `[1]` stack count minus 1, `[2]` internal time, `[3]` cumulative time, `[4]` dict of caller counts.

Key methods:
- `run(cmd)` — Execute string `cmd` under the profiler.
- `runcall(func, *args, **kw)` — Profile a single function call.
- `runctx(cmd, globals, locals)` — Profile `cmd` with custom globals and locals.
- `print_stats(sort=-1)` — Print statistics. `sort` can be `-1` (standard name), `0` (calls), `1` (time), `2` (cumulative).
- `dump_stats(file)` — Save statistics to a file object.
- `calibrate(m, verbose=0)` — Calibrate the profiler timer.
- `create_stats()` — Create and return a stats object.
- `snapshot_stats()` — Take a snapshot of current stats.

### Module-level functions
- `run(statement, filename=None)` — Execute `statement` and gather profile stats. If `filename` is given, save stats to that file; otherwise print a report.
- `runctx(statement, globals, locals, filename=None)` — Like `run()` but with custom global and local dictionaries.

## Examples
python
import profile

# Profile a simple statement
profile.run('sum(range(100000))')

# Profile a function call
def heavy():
    return sum(i*i for i in range(10000))
pr = profile.Profile()
pr.runcall(heavy)
pr.print_stats(sort='time')

# Save stats to file
pr.dump_stats(open('myprof.stats', 'w'))
## See Also
- [Python documentation](https://docs.python.org/3.10/library/profile.html)