網站首頁 編程語言 正文
簡介
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運行結果:
補充
下面小編為大家整理了一下memory_profiler的一些使用
1、直接打印結果到終端上
#coding:utf8
from memory_profiler import profile
@profile
def test1():
c=list()
for item in range(10000):
c.append(item)
if __name__=='__main__':
test1()
結果如下
Filename: D:/python/test_sip/test_check_es.py?
?
Line # ? ?Mem usage ? ?Increment ? Line Contents?
================================================?
? ?474 ? ? 16.6 MiB ? ? 16.6 MiB ? @profile?
? ?475 ? ? ? ? ? ? ? ? ? ? ? ? ? ? def test1():?
? ?476 ? ? 16.6 MiB ? ? ?0.0 MiB ? ? ? c=list()?
? ?477 ? ? 17.0 MiB ? ? ?0.0 MiB ? ? ? for item in range(10000):?
? ?478 ? ? 17.0 MiB ? ? ?0.1 MiB ? ? ? ? ? c.append(item)?
2、定義輸出到文件,定義結果保留的小數位
#coding:utf8
from memory_profiler import profile
@profile(precision=4,stream=open('memory_profiler.log','w+'))
def test1():
c=list()
for item in range(10000):
c.append(item)
if __name__=='__main__':
test1()
結果如下
Filename: D:/python/test_sip/test_check_es.py?
?
Line # ? ?Mem usage ? ?Increment ? Line Contents?
================================================?
? ?474 ?16.5391 MiB ?16.5391 MiB ? @profile(precision=4,stream=open('memory_profiler.log','w+'))?
? ?475 ? ? ? ? ? ? ? ? ? ? ? ? ? ? def test1():?
? ?476 ?16.5430 MiB ? 0.0039 MiB ? ? ? c=list()?
? ?477 ?16.8906 MiB ? 0.0039 MiB ? ? ? for item in range(10000):?
? ?478 ?16.8906 MiB ? 0.0391 MiB ? ? ? ? ? c.append(item)?
原文鏈接:https://blog.csdn.net/hzblucky1314/article/details/125494000
相關推薦
- 2023-02-09 React?Context原理深入理解源碼示例分析_React
- 2022-06-20 go程序部署到linux上運行的實現方法_Golang
- 2022-06-02 了解Kubernetes中的Service和Endpoint_云和虛擬化
- 2022-05-12 Ubuntu 20.04開啟root用戶并恢復rc.local開機自啟
- 2022-02-22 el-step中多個步驟中的表單一起提交
- 2022-07-27 Python實現倉庫管理系統_python
- 2023-11-21 NVIDIA jetson nano/ Linux/ Ubuntu18.0.4 配置固定IP靜態IP
- 2022-05-24 Windows環境bat腳本獲取文件的創建時間_DOS/BAT
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支