How do I profile memory usage in Python?

前端 未结 8 661
无人及你
无人及你 2020-11-22 09:50

I\'ve recently become interested in algorithms and have begun exploring them by writing a naive implementation and then optimizing it in various ways.

I\'m already f

8条回答
  •  臣服心动
    2020-11-22 10:17

    Below is a simple function decorator which allows to track how much memory the process consumed before the function call, after the function call, and what is the difference:

    import time
    import os
    import psutil
     
     
    def elapsed_since(start):
        return time.strftime("%H:%M:%S", time.gmtime(time.time() - start))
     
     
    def get_process_memory():
        process = psutil.Process(os.getpid())
        mem_info = process.memory_info()
        return mem_info.rss
     
     
    def profile(func):
        def wrapper(*args, **kwargs):
            mem_before = get_process_memory()
            start = time.time()
            result = func(*args, **kwargs)
            elapsed_time = elapsed_since(start)
            mem_after = get_process_memory()
            print("{}: memory before: {:,}, after: {:,}, consumed: {:,}; exec time: {}".format(
                func.__name__,
                mem_before, mem_after, mem_after - mem_before,
                elapsed_time))
            return result
        return wrapper
    

    Here is my blog which describes all the details. (archived link)

提交回复
热议问题