Python - Performance & Profiling

Overview

Estimated time: 25–40 minutes

Performance starts with measurement. Learn to profile code, identify bottlenecks, and apply targeted optimizations.

Learning Objectives

  • Use timeit for micro-benchmarks and cProfile for whole-program profiling.
  • Differentiate CPU-bound vs I/O-bound workloads and choose the right tool.
  • Apply data-structure and algorithmic improvements before micro-optimizations.

Examples

import timeit
print(timeit.timeit("sum(range(1000))", number=1000))

Guidance & Patterns

  • Profile representative workloads; avoid optimizing cold paths.
  • Prefer built-ins and library functions implemented in C.
  • Consider vectorization (NumPy) for heavy numeric loops.

Best Practices

  • Keep benchmarks in CI for regressions; document performance budgets.
  • Cache results where appropriate (functools.lru_cache).

Exercises

  1. Profile a text-processing script and remove the top two bottlenecks.
  2. Compare list vs deque for queue workloads.