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)