How do I profile memory usage in Python?

前端 未结 8 723
无人及你
无人及你 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:11

    A simple example to calculate the memory usage of a block of codes / function using memory_profile, while returning result of the function:

    import memory_profiler as mp
    
    def fun(n):
        tmp = []
        for i in range(n):
            tmp.extend(list(range(i*i)))
        return "XXXXX"
    

    calculate memory usage before running the code then calculate max usage during the code:

    start_mem = mp.memory_usage(max_usage=True)
    res = mp.memory_usage(proc=(fun, [100]), max_usage=True, retval=True) 
    print('start mem', start_mem)
    print('max mem', res[0][0])
    print('used mem', res[0][0]-start_mem)
    print('fun output', res[1])
    

    calculate usage in sampling points while running function:

    res = mp.memory_usage((fun, [100]), interval=.001, retval=True)
    print('min mem', min(res[0]))
    print('max mem', max(res[0]))
    print('used mem', max(res[0])-min(res[0]))
    print('fun output', res[1])
    

    Credits: @skeept

提交回复
热议问题