Python Tutorial 0/52 lessons ~6 min read Lesson 51
Performance Optimization
Profile first, optimize second.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Interview Q&A included
Introduction
Profile first, optimize second. Python's tools: cProfile, timeit, py-spy. Speed wins: vectorize, cache, drop to C/Rust extensions.
Understanding the topic
Core concepts to understand:
cProfile— full call profile.py-spy— sampling, no-overhead, prod-safe.- Vectorize with NumPy/Pandas.
@lru_cachefor pure functions.- Cython / Rust (PyO3) for hot loops.
Syntax reference
Visual flow / code:
bash
# cProfilepython -m cProfile -s tottime main.py# Timeit a snippetpython -m timeit -n 1000 "sum(range(1000))"# py-spy on a running processpy-spy record -o flame.svg --pid 1234# Memoizationfrom functools import lru_cache@lru_cachedef fib(n):return n if n < 2 else fib(n-1) + fib(n-2)
Execution workflow
1Performance Optimization Workflow
1 / 4cProfile
full call profile.
Real-world use
When pure Python isn't fast enough: Cython (typed Python), Numba (JIT for NumPy), or Rust extensions via maturin + PyO3.
Best practices
- Profile before optimizing.
- Pick the right data structure first.
- Vectorize and cache before going native.
Common mistakes
- Premature optimization — measure first.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionHow do you profile Python?+
Answer
Measure before optimizing. Use
cProfile (with pstats or a viewer such as snakeviz) for function-level time, timeit for micro-benchmarks, tracemalloc for memory allocations, and sampling profilers like py-spy for live processes. Line-level tools such as line_profiler help locate hot lines. Profile realistic workloads and fix the biggest bottleneck first.Follow-up
What is the difference between a deterministic and a sampling profiler?
2IntermediateQuestionVectorization — why fast?+
Answer
Vectorized operations apply one operation to a whole array in compiled code, so the per-element Python interpreter overhead (type checks, bytecode dispatch, object allocation) is paid once rather than per item. Data sits in contiguous typed memory, which is cache-friendly and can use CPU SIMD instructions. The benefit is largest for numeric work; it can use extra memory for temporaries.
Follow-up
When does vectorization stop helping?
3AdvancedQuestionWhen use Cython/Rust?+
Answer
Reach for them after profiling shows a CPU-bound hot spot that algorithmic changes, better data structures, or NumPy cannot fix. Cython compiles Python-like code with optional C types and is incremental. Rust, usually via PyO3 and maturin, gives memory safety and good performance for larger native components. Both add build and maintenance cost, so keep the boundary coarse to limit call overhead.
Follow-up
What alternatives exist before writing native code, such as Numba?
Summary
In summary: Measure → optimize → re-measure. Vectorize, cache, then go native.
Ready to mark this lesson complete?Track your journey across the entire course.