Python:memory_profiler查看代码运行占用内存情况

简介:memory_profiler是第三方模块,用于监视进程的内存消耗以及python程序内存消耗的逐行分析。它是一个纯python模块,依赖于psutil模块。

安装:

pip install memory_profiler

使用方法:

1、通过装饰器运行

@profile
def func1():

2、通过命令行运行

python -m memory_profiler test_code.py

案例源码:

# -*- coding: utf-8 -*-
# time: 2022/6/11 21:17
# file: test_code.py
# 公众号: 玩转测试开发
from memory_profiler import profile

loop = 50000


@profile
def func1():
    s1 = [i for i in range(loop)]
    s2 = []
    for i in range(loop):
        if i & 1 == 1:
            s2.append(i)
    result = sum(s1) + sum(s2)
    del s1
    del s2
    return result


if __name__ == '__main__':
    result = func1()
    print(result)

方法1运行结果:

图片

方法2运行结果:

在这里插入图片描述

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值