python?memory_profiler庫(kù)生成器和迭代器內(nèi)存占用的時(shí)間分析
不進(jìn)行計(jì)算時(shí),生成器和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()
顯示結(jié)果的含義:第一列表示已分析代碼的行號(hào),第二列(Mem 使用情況)表示執(zhí)行該行后 Python 解釋器的內(nèi)存使用情況。第三列(增量)表示當(dāng)前行相對(duì)于最后一行的內(nèi)存差異。最后一列(行內(nèi)容)打印已分析的代碼。
分析:在不進(jìn)行計(jì)算的情況下,列表list和迭代器會(huì)占用空間,但對(duì)于生成器不會(huì)占用空間
當(dāng)需要計(jì)算時(shí),list和生成器的花費(fèi)時(shí)間和占用內(nèi)存
使用sum內(nèi)置函數(shù),list和生成器求和10000000個(gè)數(shù)據(jù),list內(nèi)存占用較大,生成器花費(fèi)時(shí)間大概是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()
比較分析,如果需要對(duì)數(shù)據(jù)進(jìn)行迭代使用時(shí),生成器方法的耗時(shí)較長(zhǎng),但內(nèi)存使用方面還是較少,因?yàn)槭褂蒙善鲿r(shí),內(nèi)存只存儲(chǔ)每次迭代計(jì)算的數(shù)據(jù)。分析原因時(shí)個(gè)人認(rèn)為,生成器的迭代計(jì)算過(guò)程中,在迭代數(shù)據(jù)和計(jì)算直接不斷轉(zhuǎn)換,相比與迭代器對(duì)象中先將數(shù)據(jù)全部保存在內(nèi)存中(雖然占內(nèi)存,但讀取比再次迭代要快),因此,生成器比較費(fèi)時(shí)間,但占用內(nèi)存小。
記錄數(shù)據(jù)循環(huán)求和500000個(gè)數(shù)據(jù),迭代器和生成器循環(huán)得到時(shí)
總結(jié):幾乎同時(shí)完成,迭代器的占用內(nèi)存較大
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計(jì)算5000000個(gè)數(shù)據(jù)時(shí)間比較
總結(jié):list+sum和迭代器+sum計(jì)算時(shí)長(zhǎng)差不多,但生成器+sum計(jì)算的時(shí)長(zhǎng)幾乎長(zhǎng)一倍,
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()
到此這篇關(guān)于python memory_profiler庫(kù)生成器和迭代器內(nèi)存占用的時(shí)間分析的文章就介紹到這了,更多相關(guān)python的memory_profiler 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)計(jì)算資源圖標(biāo)crc值的方法
這篇文章主要介紹了python實(shí)現(xiàn)計(jì)算資源圖標(biāo)crc值的方法,通過(guò)解析資源文件找到icon的數(shù)據(jù),從而實(shí)現(xiàn)該功能,需要的朋友可以參考下2014-10-10python設(shè)置Pyplot的動(dòng)態(tài)rc參數(shù)、繪圖的填充
本文主要介紹了python設(shè)置Pyplot的動(dòng)態(tài)rc參數(shù)、繪圖的填充,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問(wèn)題
這篇文章主要介紹了pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02keras 獲取某層輸出 獲取復(fù)用層的多次輸出實(shí)例
這篇文章主要介紹了keras 獲取某層輸出 獲取復(fù)用層的多次輸出實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05Python random模塊用法解析及簡(jiǎn)單示例
這篇文章主要介紹了Python random模塊用法解析及簡(jiǎn)單示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Python使用pyinstaller打包成.exe文件執(zhí)行后閃退的圖文解決辦法
這篇文章主要給大家介紹了關(guān)于Python使用pyinstaller打包成.exe文件執(zhí)行后閃退的圖文解決辦法,閃退問(wèn)題通常是由于程序運(yùn)行過(guò)程中出現(xiàn)了未處理的異常或錯(cuò)誤,導(dǎo)致程序崩潰,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12