0

I need to measure the CPU and RAM usage of a python script I'm writing and I'm wondering how you do this?

Say you had a simple python script like this:

a = 5
b = 6
c = 3

result = (a * b) + c
print(result)

How would you measure the CPU and RAM usage? Obviously the code I'm gonna be working with is more complicated than this but this is just to get an idea. I know psutil is a good library to use but I'm unsure where to place it in the code. Do I initialise it at the start of the file before executing the arithmetic operations or do I call it at the end?

I've tried using tracemalloc and psutil. I've looked at how to use psutil but can't find anywhere where they perform operations and then measure it using psutil, they just create an empty python file and call the psutil.

Niall
  • 1
  • 1
  • What's wrong about `tracemalloc` ? – Maurice Meyer Feb 12 '23 at 13:02
  • Does this help [How to get current CPU and RAM usage in Python?](https://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python) – DarrylG Feb 12 '23 at 13:26
  • @MauriceMeyer, what is the default measurement for tracemalloc when retrieving the current and peak RAM usage? MB, GB, KiB? – Niall Feb 12 '23 at 13:40
  • Bytes, That's why there are [docs](https://docs.python.org/3/library/tracemalloc.html#tracemalloc.Statistic.size) – Maurice Meyer Feb 12 '23 at 13:55

0 Answers0