Reputation: 119
I have written a script for processing the syslog and I need to calculate the amount of CPU usage and the memory used by this script for its completion. Is there any packages that can be used to achieve the above task?
I'm not looking for the current CPU usage of the entire system but how much of the CPU and RAM is used by the current running python script.
Somethings like if I run the script, how much time it took for its completion and the amount of RAM and CPU used by it in that time interval.
I'm new to the concepts of python and having a difficult time understanding this. Is there any method to do this?
Thanks in advance :)
Upvotes: 0
Views: 2677
Reputation: 1302
Use 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())
Install psutil by using pip install psutil
Documentation
Upvotes: 1
Reputation: 97
You can use a technique called profiling and some functions e.g. timeit
meant to calculate the performance of your scripts and the time it takes to run them.
For example, have a look at this:
How can you profile a Python script?
Upvotes: 0