直观热力图视化python代码性能分析

来源:github - csurfer/pyheat
用途:可以学习的简单开源代码

效果

以热力图的形式直观地显示代码行的性能影响,颜色越深占用时间越长。


安装与使用

⭐️ 安装:
pip install py-heat

⭐️ 简单的命令行使用:
pyheat --pyfile <path_to_python_file>

简单分析

是一个单文件应用,一共155行,简洁明了,使用 pprofile性能分析库和matplotlib.pyplot画图,将二者组合的创意很好。画图形式值得学习,利用matplotlib.pyplot.pcolor(example)和matplotlib.pyplot.colorbar以及matplotlib.pyplot.text实现效果。
不过,在win10上测试,pprofile的分析不是很稳定。

画图

简单测试

测试代码threads.py

#!/usr/bin/env python
import threading
import time

def func():
  time.sleep(1)

def func2():
  pass

t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)
t1.start()
t2.start()
(func(), func2())
t1.join()
t2.join()

命令行执行pyheat --pyfile threads.py测试结果(win10, py3.5)

其它

python性能分析工具

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容