python-performance-optimization

Profile and optimize Python code using cProfile, memory profilers, and benchmarking patterns.

Updated Mar 3, 2026
One-click install
npx skills add https://github.com/Devil-2621/gsr-research-model --skill python-performance-optimization-devil-2621
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/Devil-2621/gsr-research-model/tree/main/.cursor/skills/python-performance-optimization
Command: npx skills add https://github.com/Devil-2621/gsr-research-model --skill python-performance-optimization-devil-2621

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow Python code and hidden memory leaks waste compute resources and degrade user experience, but finding the real bottleneck without proper profiling is guesswork. This Skill provides a systematic workflow for measuring, diagnosing, and fixing CPU, memory, and I/O performance issues in Python applications. ## Core Features & Use Cases - CPU and Line Profiling: Use cProfile, line_profiler, and py-spy to identify time-consuming functions at function-level or line-level granularity, including live production processes. - Memory Optimization: Track allocations with memory_profiler and tracemalloc, detect leaks, and reduce footprint with generators, slots, and weak references. - Optimization Patterns: Apply proven techniques such as list comprehensions, dict lookups, lru_cache, NumPy vectorization, multiprocessing for CPU-bound work, and async I/O for network-bound tasks. - Use Case: A data pipeline takes 40 minutes to run. Profile it with cProfile to find a hot loop, replace list searches with dict lookups, vectorize numeric work with NumPy, and benchmark the improvement with timeit. ## Quick Start Profile my Python script with cProfile, identify the slowest functions, and suggest concrete optimizations with benchmark comparisons.

Frequently Asked Questions about python-performance-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I profile Python code to find performance bottlenecks?

Use cProfile to profile Python code by wrapping execution in a Profile object or running python -m cProfile on your script. Sort results by cumulative time with pstats to see which functions consume the most execution time.

How to check memory usage of a Python function?

Use memory_profiler with the @profile decorator to see line-by-line memory consumption, or use tracemalloc to take snapshots before and after execution and compare allocations. Both reveal where large objects are created.

cProfile vs py-spy: which profiler should I use?

cProfile is a deterministic built-in profiler best for development and scripts, while py-spy is a sampling profiler that attaches to running production processes without code changes or restarts. Use py-spy for live systems and cProfile for local analysis.

When should I use multiprocessing vs asyncio in Python?

Use multiprocessing for CPU-bound tasks like heavy computation, since it bypasses the GIL with separate processes. Use asyncio with libraries like aiohttp for I/O-bound work such as concurrent HTTP requests, where tasks wait on network or disk.

Why is my Python list search so slow?

List membership tests are O(n) because Python scans every element. Convert the data to a dict or set for O(1) lookups, which can be orders of magnitude faster for repeated membership checks on large collections.

Does lru_cache work on all Python functions?

functools.lru_cache works on functions with hashable arguments and is most effective for deterministic, repeatedly-called functions like recursive algorithms. It does not help functions with side effects or unhashable arguments such as lists.