如何计算python脚本使用的CPU和内存量?

问题描述 投票:0回答:2

我已经编写了一个用于处理syslog的脚本,我需要计算该脚本完成时所用的CPU使用量和内存。是否可以使用任何软件包来完成上述任务?

我是python概念的新手,很难理解这一点。有什么方法可以做到这一点?

提前感谢:)

python memory cpu-usage
2个回答
0
投票

您可以使用称为profiling的技术和一些功能,例如timeit旨在计算脚本的性能以及运行它们所需的时间。例如,看一下:How can you profile a Python script?


0
投票

使用psutil:

import psutil
# gives a single float value
psutil.cpu_percent()
# gives an object with many fields
psutil.virtual_memory()
# you can convert that object to a dictionary 
dict(psutil.virtual_memory()._asdict())

使用pip install psutil安装psutil

© www.soinside.com 2019 - 2024. All rights reserved.