| name | maverick-python-performance |
| description | Python performance optimization and profiling Use when this capability is needed. |
| metadata | {"author":"get2knowio"} |
Python Performance Skill
Performance optimization patterns and profiling techniques.
List Comprehensions vs Loops
squares = [x**2 for x in range(1000)]
squares = []
for x in range(1000):
squares.append(x**2)
Generator Expressions (Lazy Evaluation)
squares = (x**2 for x in range(1_000_000))
for square in squares:
if square > 1000:
break
String Concatenation
result = ""
for item in items:
result += str(item) + ","
result = ",".join(str(item) for item in items)
Dictionary Lookups
value = d.get(key, default_value)
from collections import defaultdict
counts = defaultdict(int)
for item in items:
counts[item] += 1
Profiling
import cProfile
import pstats
cProfile.run('my_function()', 'output.prof')
stats = pstats.Stats('output.prof')
stats.sort_stats('cumulative').print_stats(10)
Review Severity
- MAJOR: O(n²) string concatenation in loop
- MINOR: List when generator would suffice
- SUGGESTION: Could use comprehension instead of loop
Converted and distributed by TomeVault — claim your Tome and manage your conversions.