python-performance-optimization
使用分析工具優化 Python 效能
也可從以下取得: ActiveInferenceInstitute
緩慢的 Python 程式碼會隱藏浪費資源和時間的瓶頸。本技能將帶您透過具體工具和模式進行分析和優化步驟。
下載技能 ZIP
在 Claude 中上傳
前往 設定 → 功能 → 技能 → 上傳技能
開啟並開始使用
測試它
正在使用「python-performance-optimization」。 Help me profile a slow data processing function and suggest improvements.
預期結果:
- Run cProfile and sort by cumulative time to find the hottest functions
- Replace list accumulation with generators where possible to reduce memory
- Consider multiprocessing for CPU bound steps after profiling confirms bottlenecks
正在使用「python-performance-optimization」。 My Python API responds slowly under load. How do I find the bottleneck?
預期結果:
- Use cProfile to identify which functions consume the most time
- Check for N+1 query patterns in database access
- Consider adding lru_cache to repeated computations
- Profile with py-spy on production if safe
正在使用「python-performance-optimization」。 Show me how to optimize memory usage in a long-running script.
預期結果:
- Use tracemalloc to track allocations and find leaks
- Replace lists with generators for streaming data
- Use __slots__ in classes to reduce per-instance memory
- Consider WeakValueDictionary for caches
安全審計
安全Documentation-only skill containing Python optimization guidance. All static findings are false positives caused by the scanner misinterpreting code examples in markdown documentation. The skill provides instructional content only; no code is executed, no network calls are made, and no files are accessed. Users receive guidance on profiling tools and optimization patterns.
風險因素
⚙️ 外部命令 (53)
品質評分
你能建構什麼
降低 API 延遲
分析緩慢的端點並應用快取或資料結構變更以獲得更快的回應。
加速批次處理作業
優化資料管線中的迴圈、記憶體使用量和 I/O 模式。
效能審查計劃
建立分析計劃並優先修復最大的瓶頸。
試試這些提示
顯示一個針對緩慢函式的最小 cProfile 範例,並說明如何解讀頂部的累積時間結果。
說明如何在單一函式上使用 line_profiler 以及如何解讀每行的執行時間。
提供使用 memory_profiler 或 tracemalloc 來找出指令碼中峰值配置的方法。
針對 CPU 密集型管線建議優化選項,包括演算法變更、快取和多程序處理。
最佳實務
- 在優化之前進行分析以確認瓶頸存在
- 對每次變更進行效能測試以驗證是否有所改善
- 將優化工作重點放在最常執行的熱路徑上
避免
- 在未先分析以識別真正瓶頸的情況下進行優化
- 在效能測試之間更改多個變數,使得難以衡量影響
- 過度優化很少執行的冷路徑程式碼