Performance Optimization
Profiling, memory optimization, caching, and performance patterns.
Overview
Master Python performance techniques.
Profiling
import cProfile
import time
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Profile execution
cProfile.run('slow_function()')
# Manual profiling
start = time.perf_counter()
slow_function()
end = time.perf_counter()
print(f"Time: {end - start:.4f}s")
Memory Optimization
import sys
# Check object size
my_list = [1, 2, 3, 4, 5]
print(sys.getsizeof(my_list)) # 104 bytes
# Use generators for large datasets
def large_dataset():
for i in range(1000000):
yield i
# Memory efficient
total = sum(large_dataset())
# Slots for classes
class Point:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
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:.4f}s")
Practice
Optimize a slow data processing pipeline.