技能 python-performance-optimization
📦

python-performance-optimization

安全

Optimize Python code performance and profiling

也可從以下取得: wshobson,ActiveInferenceInstitute

Slow Python code wastes resources and frustrates users. This skill provides systematic profiling and optimization techniques to identify bottlenecks and improve performance.

支援: Claude Codex Code(CC)
🥉 75 青銅
1

下載技能 ZIP

2

在 Claude 中上傳

前往 設定 → 功能 → 技能 → 上傳技能

3

開啟並開始使用

測試它

正在使用「python-performance-optimization」。 Profile a function that processes a large list

預期結果:

Timing report showing process_data() consumed 2.3s (85% of total time), with 90% spent in nested loops. Recommendation: Replace O(n²) algorithm with dictionary lookup for O(n) performance.

正在使用「python-performance-optimization」。 Analyze memory usage pattern

預期結果:

Memory profile shows 500MB allocation in data_cache dictionary that grows unbounded. Recommendation: Implement LRU cache with maxsize parameter or use WeakValueDictionary for automatic cleanup.

安全審計

安全
v1 • 2/24/2026

Static analysis flagged 68 patterns but all are false positives. The backtick detections are markdown code formatting, not shell execution. Network and URL findings are educational examples using test endpoints. SQLite references are documentation examples. System reconnaissance patterns are legitimate profiling tool demonstrations. This is a documentation-only skill with no executable code or security risks.

2
已掃描檔案
908
分析行數
0
發現項
1
審計總數
未發現安全問題
審計者: claude

品質評分

38
架構
100
可維護性
87
內容
50
社群
100
安全
100
規範符合性

你能建構什麼

Debug slow API endpoints

Profile a web application to identify slow database queries and optimize response times.

Optimize data processing pipelines

Analyze memory usage and execution time of ETL workflows to reduce resource consumption.

Improve algorithm efficiency

Profile computational code to identify inefficient patterns and apply appropriate optimizations.

試試這些提示

Basic profiling setup
Help me profile this Python function to identify performance bottlenecks. Here is my code: [paste code]. Show me how to use cProfile to measure execution time and find slow functions.
Memory leak investigation
My Python application uses increasing memory over time. Help me use memory_profiler and tracemalloc to identify memory leaks in this code: [paste code]. Explain what the output means.
Optimization recommendations
I have profiled my code and found these bottlenecks: [paste profiling output]. Recommend specific optimization techniques and show before/after code examples for each suggestion.
Advanced production profiling
Guide me through setting up py-spy to profile a running Python production service without stopping it. Explain how to generate flame graphs and interpret the results to find hot paths.

最佳實務

  • Always profile before optimizing to identify actual bottlenecks, not assumed ones
  • Use appropriate data structures: dictionaries for lookups, sets for membership tests, generators for large sequences
  • Cache expensive computations with functools.lru_cache and batch I/O operations to reduce system call overhead

避免

  • Optimizing code without profiling data leads to wasted effort on non-bottlenecks
  • String concatenation in loops using + operator instead of join() causes quadratic time complexity
  • Loading entire files or datasets into memory when iterator-based processing would suffice

常見問題

Which profiler should I use first?
Start with cProfile for overall CPU profiling. It is built into Python and shows which functions consume the most time. Use line_profiler for detailed line-by-line analysis of specific hot functions.
How do I profile production code without impacting performance?
Use py-spy, a sampling profiler that attaches to running processes with minimal overhead. It can generate flame graphs and does not require code changes or restarts.
Why is my optimized code slower than the original?
Common causes include premature optimization of cold paths, added complexity causing cache misses, or micro-optimizations that interfere with Python's internal optimizations. Always benchmark changes.
When should I use multiprocessing versus threading?
Use multiprocessing for CPU-bound tasks to bypass the GIL and utilize multiple cores. Use threading or asyncio for I/O-bound tasks where the GIL is released during I/O operations.
How can I reduce memory usage in my Python application?
Use generators instead of lists for large sequences, implement __slots__ in classes with many instances, avoid keeping unnecessary references, and use weak references for caches.
Is NumPy always faster than pure Python?
NumPy excels at vectorized numerical operations on large arrays due to C implementation. For small datasets or non-numerical tasks, pure Python may be simpler and equally fast.