網(wǎng)站首頁 編程語言 正文
不進行計算時,生成器和list空間占用
import time
from memory_profiler import profile
@profile(precision=4)
def list_fun():
start = time.time()
total = ([i for i in range(5000000)])
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = (i for i in range(5000000))
print('gent_spend_time:',time.time()-gent_start)
iter_fun()
gent_func()
顯示結果的含義:第一列表示已分析代碼的行號,第二列(Mem 使用情況)表示執(zhí)行該行后 Python 解釋器的內存使用情況。第三列(增量)表示當前行相對于最后一行的內存差異。最后一列(行內容)打印已分析的代碼。
分析:在不進行計算的情況下,列表list和迭代器會占用空間,但對于生成器不會占用空間
當需要計算時,list和生成器的花費時間和占用內存
使用sum內置函數(shù),list和生成器求和10000000個數(shù)據(jù),list內存占用較大,生成器花費時間大概是list的兩倍
import time
from memory_profiler import profile
@profile(precision=4)
def iter_fun():
start = time.time()
total = sum([i for i in range(10000000)])
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = sum(i for i in range(10000000))
print('gent_spend_time:',time.time()-gent_start)
iter_fun()
gent_func()
比較分析,如果需要對數(shù)據(jù)進行迭代使用時,生成器方法的耗時較長,但內存使用方面還是較少,因為使用生成器時,內存只存儲每次迭代計算的數(shù)據(jù)。分析原因時個人認為,生成器的迭代計算過程中,在迭代數(shù)據(jù)和計算直接不斷轉換,相比與迭代器對象中先將數(shù)據(jù)全部保存在內存中(雖然占內存,但讀取比再次迭代要快),因此,生成器比較費時間,但占用內存小。
記錄數(shù)據(jù)循環(huán)求和500000個數(shù)據(jù),迭代器和生成器循環(huán)得到時
總結:幾乎同時完成,迭代器的占用內存較大
import time
from memory_profiler import profile
itery = iter([i for i in range(5000000)])
gent = (i for i in range(5000000))
@profile(precision=4)
def iter_fun():
start = time.time()
total= 0
for item in itery:
total+=item
print('iter:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = 0
for item in gent:
total+=item
print('gent:',time.time()-gent_start)
iter_fun()
gent_func()
list,迭代器和生成器共同使用sum計算5000000個數(shù)據(jù)時間比較
總結:list+sum和迭代器+sum計算時長差不多,但生成器+sum計算的時長幾乎長一倍,
import time
from memory_profiler import profile
@profile(precision=4)
def list_fun():
start = time.time()
print('start!!!')
list_data = [i for i in range(5000000)]
total = sum(list_data)
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def iter_fun():
start = time.time()
total = 0
total = sum(iter([i for i in range(5000000)]))
print('total:',total)
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = sum(i for i in range(5000000))
print('total:',total)
print('gent_spend_time:',time.time()-gent_start)
list_fun()
iter_fun()
gent_func()
原文鏈接:https://blog.csdn.net/weixin_43794311/article/details/125462691
相關推薦
- 2022-03-03 【css】page-break-after 頁面打印分頁屬性
- 2022-09-21 flutter實現(xiàn)底部不規(guī)則導航欄_Android
- 2022-06-23 Python基于鏈接表實現(xiàn)無向圖最短路徑搜索_python
- 2022-10-23 Android性能優(yōu)化全局異常處理詳情_Android
- 2023-04-24 FFmpeg實戰(zhàn)之分離出PCM數(shù)據(jù)_C 語言
- 2022-06-01 Kubernetes集群的組成介紹_云和虛擬化
- 2022-06-12 Spring Boot自定義Start組件開發(fā)實現(xiàn)
- 2021-12-16 Warning: Can‘t perform a React state update on an
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支