python-performance-optimization

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

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill python-performance-optimization-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/python-performance-optimization
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill python-performance-optimization-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires line-profiler, memory-profiler, py-spy, numpy, aiohttp, requests, pytest-benchmark, and includes references (resource) components.

What problem does it solve? Slow Python code and hidden memory leaks are hard to diagnose without measurement. This Skill provides a systematic workflow for profiling CPU and memory usage, identifying bottlenecks, and applying proven optimization patterns to make Python applications faster and more memory-efficient. ## Core Features & Use Cases - CPU and Line Profiling: Use cProfile, pstats, and line_profiler to pinpoint time-consuming functions down to individual lines. - Memory Analysis: Track allocations and detect leaks with memory_profiler, tracemalloc, and weak references. - Optimization Patterns: Apply concrete techniques like list comprehensions, generators, lru_cache, NumPy vectorization, multiprocessing, async I/O, and batch database operations. - Use Case: A data processing pipeline takes 10 minutes to run. Profile it with cProfile to find the hot loop, replace list searches with dict lookups, and vectorize numerical work with NumPy to cut runtime dramatically. ## Quick Start Profile my Python script with cProfile and suggest optimizations for the slowest functions.

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 script.py. Sort results by cumulative time with pstats to identify the functions consuming the most execution time.

How to find memory leaks in Python applications?

Use tracemalloc to take memory snapshots before and after code execution, then compare them to find the largest allocations. The memory_profiler package with its @profile decorator also shows per-line memory usage.

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

cProfile measures function-level CPU time and is built into Python. line_profiler gives line-by-line timing for granular analysis. py-spy samples running processes without code changes, making it suitable for production profiling.

When should I use multiprocessing vs async I/O in Python?

Use multiprocessing for CPU-bound tasks like heavy computation, since it bypasses the GIL with separate processes. Use async I/O with asyncio for I/O-bound work like HTTP requests, where tasks wait on external responses.

Why is my Python list search slow and how do I fix it?

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 yield orders-of-magnitude speedups on large collections.