Performance and profiling

Performance and profiling

Stay up-to-date with the latest Python features and best practices. This section covers new enhancements in Python 3.13 and 3.14, performance profiling, and common pitfalls to avoid, ensuring you write clean and efficient code.

6 audio · 1:45

Nortren·

How do you measure the performance of Python code?

0:16
For small snippets, use the timeit module or the percent timeit magic in IPython. For full programs, use cProfile to find hot functions, line_profiler for line-level detail, and memory_profiler for memory usage. Always measure before optimizing; intuition about Python performance is often wrong.

What is cProfile?

0:15
cProfile is the built-in deterministic profiler. It records every function call and reports total time, cumulative time, and call counts. The output can be analyzed with the pstats module or visualized with tools like snakeviz. It has lower overhead than the older profile module.

What is the difference between time complexity of list and dict operations?

0:17
List access by index, append, and pop from the end are constant time. Insertion and deletion at the front are linear time. Membership testing is linear time. Dict access, insertion, and deletion are average constant time, as is membership testing. For frequent membership tests, prefer sets or dicts over lists.

How do you optimize Python code?

0:19
First, profile to find bottlenecks. Then consider: using better algorithms or data structures, replacing hot loops with vectorized operations using NumPy, using built-in functions and comprehensions instead of manual loops, caching expensive results with functools.cache, and as a last resort, rewriting hot paths in C, Cython, or Rust.

What is the JIT compiler in Python 3.13 and 3.14?

0:17
Python 3.13 introduced an experimental copy-and-patch JIT compiler. Python 3.14 improved it further, with measurable speedups on some workloads. As of 2026, the JIT is still being refined and is not enabled by default in standard builds. It is part of the broader Faster CPython project.

What is the Faster CPython project?

0:21
Faster CPython is a multi-year effort started in 2021 to make CPython significantly faster. It has delivered substantial speedups since Python 3.11, including specialized adaptive interpretation, the JIT compiler in 3.13 and 3.14, and other interpreter improvements. The goal is to make idiomatic Python code faster without changing the language. ---