程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python:memory_ Profiler view the memory occupied by code operation

編輯:Python

brief introduction :memory_profiler Is a third-party module , Used to monitor the memory consumption of the process and python Line by line analysis of program memory consumption . It's a pure python modular , Depend on psutil modular .

install :

pip install memory_profiler

Usage method :

1、 Run through finisher

@profile
def func1():

2、 Run... From the command line

python -m memory_profiler test_code.py

Source code of case :

# -*- coding: utf-8 -*-
# time: 2022/6/11 21:17
# file: test_code.py
# official account : Play with test development 
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)

Method 1 Running results :

Method 2 Running results :

WeChat official account : Play with test development
Welcome to your attention , Common progress , thank you !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved