Python Advanced Lesson 30

Python Performance and Debugging

Improve programs with profiling, Big O thinking, logging, debugging, and memory awareness.

Performance Starts With Measurement

Guessing is a weak way to optimize. Measure first, then improve the part that actually matters.

import time

start = time.perf_counter()
run_work()
elapsed = time.perf_counter() - start
print(elapsed)

For deeper analysis, use profilers such as cProfile.

Big O Thinking

Big O describes how work grows as input grows.

An O(n) loop grows roughly with the number of items. An O(n²) nested loop can become expensive quickly.

for a in items:
    for b in items:
        compare(a, b)

This pattern may be fine for tiny lists but painful for large inputs.

Use Better Data Structures

Choosing a set instead of a list can make membership checks much faster for large collections.

blocked = set(blocked_users)

if user in blocked:
    ...

Good data structures often beat clever micro-optimizations.

Debugging Tools

Use print() for quick learning, but use logging and debuggers for larger programs.

import logging

logging.info("Loaded %s rows", row_count)

The pdb debugger can pause execution and let you inspect variables step by step.

Memory Awareness

Large lists can use a lot of memory. Generators help when values can be produced one at a time.

squares = (number * number for number in range(1_000_000))

This does not build a million-item list immediately.

Common Mistakes

A common mistake is optimizing code before knowing it is slow. Another is making code harder to read for a tiny speed improvement.

Also avoid logging sensitive information. Debug output should help development without exposing private data.

Quick Summary

  • Measure before optimizing.
  • time.perf_counter() can time code sections.
  • cProfile helps find hot spots.
  • Big O describes growth as input size increases.
  • Better data structures can improve performance.
  • Logging is better than scattered print statements in larger programs.
  • Generators can reduce memory use.

Practice Quiz

Now practice this topic with MCQs and explanations:

Start the Python Performance and Debugging quiz