美文网首页想法散文简友广场
python代码内存占用、执行时长、运行频率、执行进度分析

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

作者: Cache_wood | 来源:发表于2021-11-16 07:58 被阅读0次

@[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代码内存占用、执行时长、运行频率、执行进度分析

    @[toc] 代码的内存占用分析 代码的执行时长分析 代码运行频率的分析 代码执行进度的可视化

  • Python内存分析

    1. 内存分析 1.1 程序运行方式 Python执行一个程序:程序就从解释器申请内存 Python解释器:预加载...

  • Analyze 静态内存分析

    静态内存分析:不运行程序,直接对代码进行分析。 程序的运行:从硬盘读取到内存里面,由CPU一句句执行代码。 静态内...

  • 算法复杂度分析(一)

    复杂度分析 数据结构和算法本身解决的是 代码执行速度快 和 节省占用的内存 的问题,那么如何衡量你写的算法的执行...

  • 性能参数

    性能指标 执行时间: 一段代码从开始运行到运行结束,所使用的时间 CPU时间: 函数或线程占用CPU的时间 内存分...

  • Swift reduce高阶函数与for循环运行效率比较

    1、打印代码执行时长的方法 2、运行代码计算0~10000整数的和 运行结果: 从运行结果可以看出,reduce高...

  • 数据结构+算法=程序

    数据结构:数据之间的关系 算法:空间复杂度(运行时占用多少内存)与时间复杂度(关键代码的执行次数) 如下,O(n)...

  • iOS之Tagged Pointer简单总结

    请问以下代码执行结果是什么: 运行结果:崩溃(坏内存访问) 原因分析:因为setter方法中,对strong修饰的...

  • Python_day2_基础知识

    执行过程 首先来说下python的执行过程: python解释器读取源代码到内存中 解释器进行词法语法检测 解释器...

  • 作用域、作用域链

    写出下面代码执行之后的结果,并分析原理 执行结果 原理分析考察点是js在浏览器中的运行机制 浏览器在运行js代码之...

网友评论

    本文标题:python代码内存占用、执行时长、运行频率、执行进度分析

    本文链接:https://www.haomeiwen.com/subject/qxawzltx.html