- 全程日志跟踪
python -m pip install pysnooper --index-url=https://pypi.tuna.tsinghua.edu.cn/simple
import pysnooper
@pysnooper.snoop(output="/main.log",
max_variable_length=None,
color=False,
relative_time=False)
linux系统下执行:
2. 统计程序执行时间
time python test.py
real 0m9.221s
user 0m8.144s
sys 0m1.047s
real:程序实际运行时间,单位为秒。
user:程序用户态运行的时间,即程序执行自身代码的时间,单位为秒。
sys:程序在内核态运行的时间,单位为秒。
- 统计程序执行占用内存
import resource
mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
...中间代码
mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print(f'Initial RAM usage: {mem_init:14,}')
print(f' Final RAM usage: {mem_final:14,}')
# 单位为KB
# mem_init:14, 意思是 mem_init占14个字符宽度