Content · Programming · Python

Python profiling.sampling: Create Flame Graphs and Heatmaps

Flame graphs show approximate hot call stacks and heatmaps associate samples with source locations.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Flame graphs show approximate hot call stacks and heatmaps associate samples with source locations.

Understanding the Concept


Flame graphs show approximate hot call stacks and heatmaps associate samples with source locations. Save them as generated diagnostic artifacts, not as trusted standalone proof.

Requires Python 3.15; Tachyon reports statistical estimates. Exact flags can be checked with python -m profiling.sampling --help.

How to interpret the measurement


Before optimizing, establish an objective measurement and a representative workload. Memory size, function-call counts, elapsed time and samples are different metrics: knowing one does not determine the others. Repeat observations before drawing a conclusion about a performance regression.

Profiling output is evidence for finding a bottleneck, not a performance guarantee. Check that the program still produces the correct result after any optimization and note version, input size and measurement conditions.

Version and Environment Requirements


Python 3.15 or newer is required for the profiling package and Tachyon commands; the local test environment uses Python 3.13.

Worked Example


Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.

# Run in a terminal with Python 3.15 or newer:
python -m profiling.sampling run --flamegraph -o profile.html sample.py
# Replace sample.py or PID 12345 with your own authorized target.

Walkthrough: What Each Part Does


  • These are terminal commands, not Python statements. Replace the example script or process ID with one you control and verify command-line options on the installed interpreter.
  • Read the profiler output as an observation of execution rather than a promise of a particular duration or percentage.

This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.

The demonstrated API requires a newer interpreter than the Python 3.13 verification environment. Its documented behavior and version requirement must be checked on the intended Python installation.

Important Behavior and Edge Cases


Treating the widest flame graph bar as a precise timing measurement of one function.

Requires Python 3.15; Tachyon reports statistical estimates. Exact flags can be checked with python -m profiling.sampling --help.

Practice Exercise


Profile a local, non-sensitive script and explain what is shown in the resulting view.

Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.

Further reading: official documentation and API reference.

Common Beginner Mistakes


  • Treating the widest flame graph bar as a precise timing measurement of one function.
  • Comparing results from different workloads or environments as if the measurements were directly interchangeable.

Key Takeaways


  • Flame graphs show approximate hot call stacks and heatmaps associate samples with source locations.
  • Requires Python 3.15; Tachyon reports statistical estimates. Exact flags can be checked with python -m profiling.sampling --help.
  • Practice: Profile a local, non-sensitive script and explain what is shown in the resulting view.
Share this resourceSend this page to someone who may find it useful.