mcpField
O
optimizing-python-performance

optimizing-python-performance

github.com/wdm0006Development109134 installsUpdated 2d ago
Repository

About optimizing-python-performance

A Python performance optimization skill for developers, covering profiling, memory analysis, benchmarking, and common code-level improvements.

Key features

  1. 01PyInstrument and cProfile profiling
  2. 02Memory analysis with tracemalloc
  3. 03pytest-benchmark usage
  4. 04Algorithm and data structure guidance
  5. 05Pre-optimization checklist

Use cases

  1. 01Locate runtime bottlenecks in Python code
  2. 02Inspect memory allocations
  3. 03Measure benchmark results and regressions

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/wdm0006/python-skills --skill optimizing-python-performance

After installing, type/optimizing-python-performancein Claude Code to confirm it loaded.

Bundled filesBENCHMARKS.mdPROFILING.md

Details

From the project README

Python Performance Optimization

Profiling Quick Start

# PyInstrument (statistical, readable output)
python -m pyinstrument script.py

# cProfile (detailed, built-in)
python -m cProfile -s cumulative script.py

# Memory profiling
uv add --dev memray
memray run script.py
memray flamegraph memray-*.bin

PyInstrument Usage

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()
result = my_function()
profiler.stop()
print(profiler.output_text(unicode=True, color=True))

Memory Analysis

import tracemalloc

tracemalloc.start()
# ... code ...
snapshot = tracemalloc.take_snapshot()
for stat in snapshot.statistics('lineno')[:10]:
    print(stat)

Benchmarking (pytest-benchmark)

def test_encode_benchmark(benchmark):
    result = benchmark(encode, 37.7749, -122.4194)
    assert len(result) == 12
pytest tests/ --benchmark-only
pytest tests/ --benchmark-compare

Common Optimizations

# Use set for membership (O(1) vs O(n))
valid = set(items)
if item in valid: ...

# Use deque for queue operations
from collections import deque
queue = deque()
queue.popleft()  # O(1) vs list.pop(0) O(n)

# Use generators for large data
def process(items):
    for item in items:
        yield transform(item)

# Cache expensive computations
from functools import lru_cache

@lru_cache(maxsize=1000)
def expensive(x):
    return compute(x)

# String building
result = "".join(str(x) for x in items)  # Not += in loop

Algorithm Complexity

Operationlistsetdict
LookupO(n)O(1)O(1)
InsertO(1)O(1)O(1)
DeleteO(n)O(1)O(1)

For detailed strategies, see:

Optimization Checklist

Before Optimizing:
- [ ] Confirm there's a real problem
- [ ] Profile to find actual bottleneck
- [ ] Establish baseline measurements

Process:
- [ ] Algorithm improvements first
- [ ] Then data structures
- [ ] Then implementation details
- [ ] Measure after each change

After:
- [ ] Add benchmarks to prevent regression
- [ ] Verify correctness unchanged
- [ ] Document why optimization needed

Learn More

This skill is based on the Performance section of the Guide to Developing High-Quality Python Libraries by Will McGinnis. See these posts for deeper coverage:

Comments

Markdown supported