关于 code-profiler
code-profiler 是用于分析 Python 性能并定位瓶颈的 skill,可测量运行时间和内存用量,并提供逐行分析、调用统计、火焰图及优化前后对比。
主要功能
- 01测量函数执行时间
- 02逐行性能分析
- 03统计调用次数与累计耗时
- 04跟踪内存分配
- 05生成火焰图
- 06比较优化前后表现
适用场景
- 01查找 Python 脚本中的慢函数
- 02检查内存用量
- 03查看函数调用路径
- 04验证优化效果
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill code-profiler安装后在 Claude Code 中输入/code-profiler确认已加载。
附带文件scripts/code_profiler.pyscripts/requirements.txt
详细介绍
代码性能分析器
分析 Python 代码性能、识别瓶颈,并通过全面的性能分析工具优化执行效率。
用途
性能分析适用于:
- 识别瓶颈
- 测量函数执行时间
- 分析内存使用情况
- 可视化调用图
- 验证优化效果
功能
- 时间分析:测量函数执行时间
- 逐行分析:分析每行代码
- 调用统计:统计函数调用次数和累计耗时
- 内存分析:跟踪内存分配和使用情况
- 火焰图可视化:可视化分析调用堆栈
- 对比:比较优化前后的效果
快速入门
from code_profiler import CodeProfiler
# Profile function
profiler = CodeProfiler()
profiler.profile_function(my_function, args=(arg1, arg2))
profiler.print_stats(top=10)
# Profile script
profiler.profile_script('script.py')
profiler.export_report('profile_report.html')
CLI 用法
# Profile Python script
python code_profiler.py script.py
# Profile with line-by-line analysis
python code_profiler.py script.py --line-by-line
# Export HTML report
python code_profiler.py script.py --output report.html
评论
支持 Markdown