Loading...
Loading...
Python performance optimization patterns using profiling, algorithmic improvements, and acceleration techniques. Use when optimizing slow Python code, reducing memory usage, or improving application throughput and latency.
npx skill4agent add nickcrew/claude-ctx-plugin python-performance-optimization| Task | Load reference |
|---|---|
| Profile code and find bottlenecks | |
| Algorithm and data structure optimization | |
| Memory optimization and generators | |
| String concatenation and file I/O | |
| NumPy, Numba, Cython, multiprocessing | |
@lru_cache# Slow: O(n) lookup
if item in large_list: # Bad
# Fast: O(1) lookup
if item in large_set: # Good# Slower
result = []
for i in range(n):
result.append(i * 2)
# Faster (35% speedup)
result = [i * 2 for i in range(n)]from functools import lru_cache
@lru_cache(maxsize=None)
def expensive_function(n):
# Result cached automatically
return complex_calculation(n)# Memory inefficient
def read_file(path):
return [line for line in open(path)] # Loads entire file
# Memory efficient
def read_file(path):
for line in open(path): # Streams line by line
yield line.strip()# Pure Python: ~500ms
result = sum(i**2 for i in range(1000000))
# NumPy: ~5ms (100x faster)
import numpy as np
result = np.sum(np.arange(1000000)**2)"".join()StringIO__slots__@lru_cache@lru_cache