pythonprofilers / memory_profiler

Monitor Memory usage of Python code
http://pypi.python.org/pypi/memory_profiler
Other
4.39k stars 380 forks source link

How to test memory usage for a time-loop function? #366

Open xushanthu-2014 opened 2 years ago

xushanthu-2014 commented 2 years ago

Hi there, I wrote a time-loop function in my script wich is like this:

import numpy as np

def time_loop():
    for i in range(0,10000000): # assume this is a time loop from x1 year x2 month x3 day to y1 year y2 month y3 day
        # some codes
        ....

if __name__ == '__main__':
    time_loop()

And I want to detect the memory usage of every line within the function time_loop. so how can I do that? does it simply follow:

import numpy as np
from memory_profiler import profile

@profile
def time_loop():
    for i in range(0,10000000): # assume this is a time loop from x1 year x2 month x3 day to y1 year y2 month y3 day
        # some codes
        ....

if __name__ == '__main__':
    time_loop()

Then python -m memory_profile myscript.py? Thanks!