python代码内存占用、执行时长、运行频率、执行进度分析

@[toc]

代码的内存占用分析

from memory_profiler import profile
@profile
def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
    del b
    return a
my_func()
Line #    Mem usage    Increment  Occurences   Line Contents
============================================================
     2     39.8 MiB     39.8 MiB           1   @profile
     3                                         def my_func():
     4     47.4 MiB      7.6 MiB           1       a = [1] * (10 ** 6)
     5    200.0 MiB    152.6 MiB           1       b = [2] * (2 * 10 ** 7)
     6     47.4 MiB   -152.6 MiB           1       del b
     7     47.4 MiB      0.0 MiB           1       return a

代码的执行时长分析

import time

#from memory_profiler import LineProfiler
from line_profiler import LineProfiler

def test_time():
    for i in range(100):
        a=[1]*(10**4)
        b=[2]*(10**4)
        pass

if __name__=='__main__':
    lp = LineProfiler()
    lp.enable_by_count()
    lp_wrapper = lp(test_time)
    test_time()
    lp.print_stats()
Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     6                                           def test_time():
     7       101        524.0      5.2      0.6         for i in range(100):
     8       100      45198.0    452.0     49.6                 a=[1]*(10**4)
     9       100      44917.0    449.2     49.3                 b=[2]*(10**4)
    10       100        555.0      5.5      0.6                 pass

代码运行频率的分析

import heartrate

heartrate.trace(browser=True)

for i in range(1000):
    pass

代码执行进度的可视化

from tqdm import tqdm
import time
import  pandas as pd
import numpy as  np

alist = list('letters')
bar = tqdm(alist)
for letter in bar:
    bar.set_description("Now get "+str(letter))
    time.sleep(1)

df = pd.DataFrame(np.random.randint(0, 100, (10000000, 60)))
#print(df.shape)
tqdm.pandas(desc="Processing...")
df.progress_apply(lambda x: x**2)
Now get s: 100%|████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:07<00:00,  1.01s/it] 
Processing...: 100%|██████████████████████████████████████████████████████████████████████████████| 60/60 [00:59<00:00,  1.02it/s]

你可能感兴趣的:(python代码内存占用、执行时长、运行频率、执行进度分析)