- Phase: 10. Concurrency & Internals
- Duration: 2 hours
- Profile Python code with cProfile and profile
- Analyze profiling results with pstats
- Benchmark small code snippets with timeit
- Identify and fix performance bottlenecks
- Understand algorithmic complexity (Big O)
- Use slots for memory optimization
- Compare list vs array vs set performance
- Profiling with cProfile and profile
- pstats for analysis
- timeit module for micro-benchmarks
- Identifying bottlenecks
- Algorithmic optimization (Big O)
- Using slots
- List vs array vs set performance
- C extensions briefly (Cython, cffi)
- PyPy as alternative interpreter
Modules 000-097.
import cProfile
import pstats
import timeit
from typing import List
# Profiling
def slow_function(n: int) -> int:
total: int = 0
for i in range(n):
total += i * i
return total
cProfile.run('slow_function(1000000)', 'profile_stats')
p = pstats.Stats('profile_stats')
p.sort_stats('cumtime').print_stats(10)
# Micro-benchmarks
time: float = timeit.timeit(
'sum(range(1000))',
number=10000
)
print(f"Average: {time / 10000:.6f}s")- Python profiling documentation
- cProfile docs: https://docs.python.org/3/library/profile.html
- timeit docs: https://docs.python.org/3/library/timeit.html
- Cython documentation: https://cython.readthedocs.io
- PyPy: https://pypy.org
Module 099: Capstone Project: Full-Stack Application