简体   繁体   中英

Python memory_profiler inconsistent plots

I have recently started using the python memory profiler from here . As a test run, I tried to profile the toy code from here following the instructions therein. I have some naive questions on the outputs I saw.

import time

@profile
def test1():
    n = 10000
    a = [1] * n
    time.sleep(1)
    return a

@profile
def test2():
    n = 100000
    b = [1] * n
    time.sleep(1)
    return b

if __name__ == "__main__":
    test1()
    test2()

This is the output using mprof run and then plot command line options: 带有功能分析

After removing the @profile lines, I ran the profiler again and obtained the following result: 没有功能分析

Except for the brackets for the functions, I was expecting almost identical plots (since the code is simple), but I am seeing some significant differences such as the ending time of the plot, variations within brackets etc.

Can someone please shed light into these differences?

Edit: For small intervals, the plot with function profiling looks like: 小间隔

The differences you are seeing are probably due to the fact that the information stored by @profile is counted within the total memory used by the program. There is also a slight overhead of storing this information, hence the different running times.

Also, you might get slightly different plots in different runs just due to variations in how Python manages memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM