Python Performance Optimization Skill:让 AI 编程助手真正学会性能调优的 profiling-first 指南
Python Performance Optimization Skill:让 AI 编程助手真正学会性能调优的 profiling-first 指南
大多数 AI 编程助手在性能问题上给建议时有一个通病:上来就告诉你”用 lru_cache“”改用 NumPy”,而不是先告诉你瓶颈在哪里。wshobson/agents 出品的 python-performance-optimization Skill 正是为解决这个问题而生——它把 profiling-first 的思维方式完整嵌入到 AI agent 的工作流里,让性能调优从”凭感觉优化”变成”先测后做”。
功能与原则
这个 Skill 的核心能力是引导 AI agent 对 Python 代码进行系统性的性能诊断与优化,覆盖 CPU profiling、内存 profiling、行级分析、调用图分析四大维度。其设计原则非常明确:永远先 profiling,再优化。在给出任何优化建议之前,Skill 会要求 agent 先用 cProfile、memory_profiler、line_profiler 或 py-spy 获取真实数据,再基于数据决策。
认可度
- GitHub Star:wshobson/agents 主仓库约 38,500 stars(截至 2026-09-13)
- 周安装量:python-performance-optimization 单 skill 约 28,000 次/周(来源:skillsmd.top)
- 所属生态:wshobson/agents 是 GitHub Star History 全球排名第 736 位的顶级项目,也是 The Agentic Leaderboard 常驻选手(最高排名第 19 位)
链接
GitHub:https://github.com/wshobson/agents
Skill 路径:plugins/python-development/skills/python-performance-optimization/
原作者
Seth Hobson(GitHub: wshobson),独立开发者,专注于为 AI 编程智能体构建生产级工作流组件。wshobson/agents 是他维护的最大项目,目标是让 AI coding agent 的工作流从”能用”升级到”专业”。
介绍
wshobson/agents 是一个多 harness 的 agent 插件市场,同时支持 Claude Code、Codex CLI、Cursor、OpenCode、GitHub Copilot 和 Gemini CLI,从同一套 Markdown 源文件生成各 harness 的原生插件。python-performance-optimization 是其 python-development 插件下的 16 个专业 skill 之一。
这个 skill 本身是一份完整的性能调优指南。文件结构分为三层:SKILL.md 承载主流程和工具分类索引;references/advanced-patterns.md 包含 NumPy vectorization、异步 I/O、并行化等进阶模式;references/details.md 则是 cProfile、memory_profiler、line_profiler、py-spy 等核心工具的逐个实战代码模板。任何支持 Agent Skills 标准的 agent(如 Claude Code、Codex)都可以通过 Skills CLI 一键安装并激活。
特点
- Profiling-first 原则:永远先测量,再优化;不基于猜测给优化建议
- 覆盖全维度性能指标:CPU 时间、内存使用、I/O 等待、调用栈关系
- 渐进式文档结构:SKILL.md → advanced-patterns.md → details.md,需要多深就读多深
- 零依赖:纯 Markdown 格式,无需安装任何框架,打开 agent context 即可使用
- 多工具链:cProfile / line_profiler / memory_profiler / py-spy 全部覆盖,含生产级 live profiling
- 跨 agent 兼容:Claude Code、Codex CLI、Cursor、OpenCode、GitHub Copilot、Gemini CLI 全部支持
- 渐进式优化策略:从算法选择 → 数据结构 → 实现模式 → 并行化 → 原生扩展,分层给出优化路径
使用方法
安装(以 Claude Code 为例):
npx skills add https://github.com/wshobson/agents --skill python-performance-optimization
或在 Claude Code 中直接对话安装:
/plugin marketplace add wshobson/agents
/plugin install python-development
激活触发:当 prompt 中涉及以下任意关键词时,agent 自动激活该 skill:
– “代码很慢”/”slow endpoint”/”性能问题”
– “内存泄漏”/”memory leak”
– “优化 throughput”/”reduce latency”
– “benchmark”/”profiling”/”瓶颈分析”
最小使用示例(让 agent 对当前代码做 CPU profiling):
帮我用 cProfile 分析这个脚本的性能,找出最耗时的前 10 个函数。
Skill 会引导 agent 生成如下流程:
import cProfile, pstats
from pstats import SortKey
profiler = cProfile.Profile()
profiler.enable()
# … 执行待分析的函数 …
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats(SortKey.CUMULATIVE)
stats.print_stats(10) # Top 10 函数
使用场景与人群
适用场景:
– Python API endpoint 响应变慢,需要定位瓶颈
– ETL / 数据处理 pipeline 执行时间超出预期
– 内存持续增长,怀疑内存泄漏
– 需要在 caching、算法调整、NumPy vectorization、并发之间做技术选型
– 生产环境 Python 进程需要 live profiling 而不中断服务
目标用户:
– 后端工程师、数据工程师、ML 工程师
– 使用 Claude Code / Codex 等 AI 编程助手的开发者
– 希望 AI agent 提供专业性能调优建议而非表面建议的团队
输入与输出案例
案例 1:定位 API 瓶颈
Input:Python Flask endpoint 运行缓慢,需要诊断
Skill 引导 agent 输出:
import cProfile, pstats
profiler = cProfile.Profile()
profiler.enable()
# 调用目标 endpoint 逻辑
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative')
stats.print_stats(15)
→ 识别出 db.query() 占用 72% 执行时间,建议添加索引或启用连接池
案例 2:NumPy 加速对比
Input:纯 Python 循环做矩阵乘法,速度太慢
Skill 引导 agent 输出:
import numpy as np
import timeit
def python_multiply():
return [x*y for x,y in zip(a,b)]
def numpy_multiply():
return np.array(a) * np.array(b)
py = timeit.timeit(python_multiply, number=100)
np = timeit.timeit(numpy_multiply, number=100)
print(f"Speedup: {py/np:.1f}x")
→ 典型场景下 NumPy 向量化可带来 50-100x 加速
评论区
登录后可评论。