Performance Patterns
Profiling, optimization, caching, and memory management.
Overview
Master performance optimization.
Profiling
import cProfile
import time
def slow_function():
return sum(range(1000000))
# Profile execution
cProfile.run('slow_function()')
# Manual timing
start = time.perf_counter()
slow_function()
elapsed = time.perf_counter() - start
print(f"Time: {elapsed:.4f}s")
Caching
from functools import lru_cache
import time
@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
start = time.time()
fibonacci(100)
print(f"Time: {time.time() - start:.6f}s")
Memory Optimization
import sys
# Use __slots__ for memory efficiency
class Point:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(1, 2)
print(sys.getsizeof(p)) # Much smaller than regular class
# Use generators for large datasets
def large_dataset():
for i in range(1000000):
yield i
# Memory efficient processing
total = sum(large_dataset())
Practice
Optimize a slow data processing pipeline.