# Optimize Python Performance

Slow Python code is hard to improve without clear measurements. This skill guides Claude, Codex, and Claude Code through profiling, benchmarking, and targeted optimization patterns.

## Install

```bash
npx skillstore add activeinferenceinstitute/python-performance-optimization
```

## Metadata

- Status: approved
- Slug: activeinferenceinstitute-python-performance-optimization
- Skillstore revision: r1
- Version status: missing
- Tree hash: 4b0784cf5458f58b3dd80864fff17ffcd1aadb1bb46065907eb99f45839ce4c7
- Author: ActiveInferenceInstitute
- GitHub username: ActiveInferenceInstitute
- License: MIT
- Repository: https://github.com/ActiveInferenceInstitute/Journal-Utilities/tree/main/.claude/skills/python-performance-optimization
- Ref: 7db9b9f06e0ab79c575b58bc48c4d8dc9849f424
- Supported tools: Claude, Codex, Claude Code
- Audit status: complete
- Agent install advisory: allowed
- Manual install advisory: allowed
- Artifact signature: available
- Audit attestation: unavailable
- Human verification: not\_verified
- Risk factors: external\_commands, network
- Quality score: 73
- Public page: https://skillstore.pages.dev/skills/activeinferenceinstitute-python-performance-optimization
- Manifest: https://skillstore.pages.dev/api/skills/activeinferenceinstitute-python-performance-optimization/manifest

## Capabilities

- Explains when to use CPU, memory, line, and call graph profiling.
- Provides cProfile, pstats, line\_profiler, memory\_profiler, and py-spy workflows.
- Shows benchmark patterns for lists, generators, strings, dictionaries, caching, and function calls.
- Covers multiprocessing for CPU-bound work and async I/O for network-bound work.
- Includes database, memory leak, iterator, weak reference, and pytest-benchmark examples.

## Use Cases

- Diagnose a slow service endpoint: Use profiling output to identify hot functions, expensive queries, or blocking I/O before changing code.
- Reduce memory pressure in data jobs: Compare list-based processing with generators, iterators, weak references, and memory profiling techniques.
- Prepare a performance improvement plan: Benchmark current behavior, choose focused optimization patterns, and measure results after each change.

## Prompt Templates

### Find my first bottleneck

```
Use the python-performance-optimization skill to help me profile this Python function. Explain which profiler to use first and what result would prove the bottleneck.
```

### Improve a slow loop

```
Use the python-performance-optimization skill to review this slow Python loop. Suggest faster data structures or built-in operations, then explain how to benchmark the change.
```

### Reduce memory usage

```
Use the python-performance-optimization skill to analyze this memory-heavy Python workflow. Identify where generators, iterators, or weak references may reduce memory use.
```

### Build a complete optimization plan

```
Use the python-performance-optimization skill to create a measurement-driven optimization plan for this Python application. Include CPU profiling, memory profiling, I/O review, database checks, and benchmark validation.
```

## Limitations

- It provides guidance and examples; it does not run profilers by itself.
- Benchmark results depend on hardware, workload size, Python version, and environment.
- Some examples require optional packages such as NumPy, py-spy, aiohttp, or pytest-benchmark.
- Production profiling needs authorization and care because process data may be sensitive.

## Best Practices

- Profile before optimizing so changes target measured bottlenecks.
- Keep benchmarks small, repeatable, and close to the real workload.
- Compare before and after results, including runtime, memory, and correctness.

## Anti Patterns

- Do not optimize code paths that are not part of the measured slowdown.
- Do not assume a microbenchmark result applies to every production workload.
- Do not attach profilers to production processes without authorization.

## Security Audit

- Audited at: 2026-07-09T01:18:34.651\+00:00
- Summary: Most static detections are false positives caused by Markdown code fences and educational Python examples. The only confirmed issue is low-severity guidance for py-spy commands that attach to live processes and may expose stack traces if used without authorization. No prompt injection, credential exfiltration, or malicious intent was found.

## Stats

- Views: 302
- Downloads: 19
- Favorites: 0
- Popularity score: 0
