技能 python-performance-optimization

python-performance-optimization

安全 ⚙️ 外部命令🌐 網路存取

使用分析工具優化 Python 效能

也可從以下取得: ActiveInferenceInstitute

緩慢的 Python 程式碼會隱藏浪費資源和時間的瓶頸。本技能將帶您透過具體工具和模式進行分析和優化步驟。

支援: Claude Codex Code(CC)
📊 69 充足
1

下載技能 ZIP

2

在 Claude 中上傳

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

3

開啟並開始使用

測試它

正在使用「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

安全審計

安全
v4 • 1/17/2026

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.

2
已掃描檔案
1,114
分析行數
2
發現項
4
審計總數
審計者: claude 查看審計歷史 →

品質評分

38
架構
100
可維護性
85
內容
23
社群
100
安全
83
規範符合性

你能建構什麼

降低 API 延遲

分析緩慢的端點並應用快取或資料結構變更以獲得更快的回應。

加速批次處理作業

優化資料管線中的迴圈、記憶體使用量和 I/O 模式。

效能審查計劃

建立分析計劃並優先修復最大的瓶頸。

試試這些提示

分析緩慢的函式
顯示一個針對緩慢函式的最小 cProfile 範例,並說明如何解讀頂部的累積時間結果。
逐行程式碼分析器指南
說明如何在單一函式上使用 line_profiler 以及如何解讀每行的執行時間。
記憶體熱點檢查
提供使用 memory_profiler 或 tracemalloc 來找出指令碼中峰值配置的方法。
優化策略
針對 CPU 密集型管線建議優化選項,包括演算法變更、快取和多程序處理。

最佳實務

  • 在優化之前進行分析以確認瓶頸存在
  • 對每次變更進行效能測試以驗證是否有所改善
  • 將優化工作重點放在最常執行的熱路徑上

避免

  • 在未先分析以識別真正瓶頸的情況下進行優化
  • 在效能測試之間更改多個變數,使得難以衡量影響
  • 過度優化很少執行的冷路徑程式碼

常見問題

這與我的 Python 版本相容嗎?
它針對現代 Python 工具;較舊版本可能需要小幅語法或工具調整。
這個技能有什麼限制?
它只提供指導和範例,不會執行分析器或修改程式碼。
它可以與我現有的工作流程整合嗎?
是的,它引用了常見工具如 cProfile、line_profiler 和 py-spy,適合大多數工作流程。
它會存取或傳送我的資料嗎?
不,它僅提供文件說明,不會讀取或傳輸任何資料。
如果分析結果不清楚怎麼辦?
分享最慢的函式和呼叫堆疊,以獲得針對性的後續步驟。
與一般性建議相比有何不同?
它專注於 Python 特定的分析器和具有真實範例的具體優化模式。