python工具——Scalene
Scalene 是一个 Python 的高性能 CPU内存分析器
用于Python脚本的CPU和内存分析器,能够正确处理多线程代码,还能区分Python代码和本机代码的运行时间
在linux或Mac OS X上运行
特点:
快速、开销少、精确
还能对高耗能的代码行进行标注
安装
pip install scalene
eg:
test.py
import time def test(): i=3; while i>0: print("test") time.sleep(10) i=i-1 test()
会生成一个文本形式的报告,显示出每一行代码的CPU和内存的使用情况
查看全部配置
python -m scalene --help
usage: scalene [-h] [--outfile OUTFILE] [--html] [--reduced-profile]
[--profile-interval PROFILE_INTERVAL] [--cpu-only]
[--profile-all] [--use-virtual-time]
[--cpu-percent-threshold CPU_PERCENT_THRESHOLD]
[--cpu-sampling-rate CPU_SAMPLING_RATE]
[--malloc-threshold MALLOC_THRESHOLD]
Scalene: a high-precision CPU and memory profiler.
https://github.com/emeryberger/scalene
% scalene yourprogram.py
optional arguments:
-h, --help show this help message and exit
--outfile OUTFILE file to hold profiler output (default: stdout)
--html output as HTML (default: text)
--reduced-profile generate a reduced profile, with non-zero lines only (default: False).
--profile-interval PROFILE_INTERVAL
output profiles every so many seconds.
--cpu-only only profile CPU time (default: profile CPU, memory, and copying)
--profile-all profile all executed code, not just the target program (default: only the target program)
--use-virtual-time measure only CPU time, not time spent in I/O or blocking (default: False)
--cpu-percent-threshold CPU_PERCENT_THRESHOLD
only report profiles with at least this percent of CPU time (default: 1%)
--cpu-sampling-rate CPU_SAMPLING_RATE
CPU sampling rate (default: every 0.01s)
--malloc-threshold MALLOC_THRESHOLD
only report profiles with at least this many allocations (default: 100)