在python編碼中for循環(huán)處理任務(wù)時(shí),會(huì)將所有的待遍歷參量加載到內(nèi)存中。
其實(shí)這本沒有必要,因?yàn)檫@些參量很有可能是一次性使用的,甚至很多場景下這些參量是不需要同時(shí)存儲(chǔ)在內(nèi)存中的,這時(shí)候就會(huì)用到本文所介紹的迭代生成器yield。
1.基本使用
首先我們用一個(gè)例子來演示一下迭代生成器yield的基本使用方法,這個(gè)例子的作用是構(gòu)造一個(gè)函數(shù)用于生成一個(gè)平方數(shù)組02,12,22...。
在普通的場景中我們一般會(huì)直接構(gòu)造一個(gè)空的列表,然后將每一個(gè)計(jì)算結(jié)果填充到列表中,最后return列表即可,對(duì)應(yīng)的是這里的函數(shù)square_number
。
而另外一個(gè)函數(shù)square_number_yield
則是為了演示yield而構(gòu)造的函數(shù),其使用語法跟return是一樣的,不同的是每次只會(huì)返回一個(gè)值:
def square_number(length):
s = []
for i in range(length):
s.append(i ** 2)
return s
def square_number_yield(length):
for i in range(length):
yield i ** 2
if __name__ == '__main__':
length = 10
sn1 = square_number(length)
sn2 = square_number_yield(length)
for i in range(length):
print (sn1[i], '\t', end='')
print (next(sn2))
在main函數(shù)中我們對(duì)比了兩種方法執(zhí)行的結(jié)果,打印在同一行上面,用end=''指令可以替代行末的換行符號(hào),具體執(zhí)行的結(jié)果如下所示:
[dechin@dechin-manjaro yield]$ python3 test_yield.py
0 0
1 1
4 4
9 9
16 16
25 25
36 36
49 49
64 64
81 81
可以看到兩種方法打印出來的結(jié)果是一樣的。也許有些場景下就是需要持久化的存儲(chǔ)函數(shù)中返回的結(jié)果,這一點(diǎn)用yield也是可以實(shí)現(xiàn)的,可以參考如下示例:
def square_number(length):
s = []
for i in range(length):
s.append(i ** 2)
return s
def square_number_yield(length):
for i in range(length):
yield i ** 2
if __name__ == '__main__':
length = 10
sn1 = square_number(length)
sn2 = square_number_yield(length)
sn3 = list(square_number_yield(length))
for i in range(length):
print (sn1[i], '\t', end='')
print (next(sn2), '\t', end='')
print (sn3[i])
這里使用的方法是直接將yield生成的對(duì)象轉(zhuǎn)化成list格式,或者用sn3 = [i for i in square_number_yield(length)]
這種寫法也是可以的,在性能上應(yīng)該差異不大。上述代碼的執(zhí)行結(jié)果如下:
[dechin@dechin-manjaro yield]$ python3 test_yield.py
0 0 0
1 1 1
4 4 4
9 9 9
16 16 16
25 25 25
36 36 36
49 49 49
64 64 64
81 81 81
2.進(jìn)階測試
在前面的章節(jié)中我們提到,使用yield可以節(jié)省程序的內(nèi)存占用,這里我們來測試一個(gè)100000大小的隨機(jī)數(shù)組的平方和計(jì)算。如果使用正常的邏輯,那么寫出來的程序就是如下所示:
import tracemalloc
import time
import numpy as np
tracemalloc.start()
start_time = time.time()
ss_list = np.random.randn(100000)
s = 0
for ss in ss_list:
s += ss ** 2
end_time = time.time()
print ('Time cost is: {}s'.format(end_time - start_time))
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats[:5]:
print (stat)
這個(gè)程序一方面通過time來測試執(zhí)行的時(shí)間,另一方面利用tracemalloc追蹤程序的內(nèi)存變化。
這里是先用np.random.randn()直接產(chǎn)生了100000個(gè)隨機(jī)數(shù)的數(shù)組用于計(jì)算,那么自然在計(jì)算的過程中需要存儲(chǔ)這些生成的隨機(jī)數(shù),就會(huì)占用這么多的內(nèi)存空間。
如果使用yield的方法,每次只產(chǎn)生一個(gè)用于計(jì)算的隨機(jī)數(shù),并且按照上一個(gè)章節(jié)中的用法,這個(gè)迭代生成的隨機(jī)數(shù)也是可以轉(zhuǎn)化為一個(gè)完整的list的:
import tracemalloc
import time
import numpy as np
tracemalloc.start()
start_time = time.time()
def ss_list(length):
for i in range(length):
yield np.random.random()
s = 0
ss = ss_list(100000)
for i in range(100000):
s += next(ss) ** 2
end_time = time.time()
print ('Time cost is: {}s'.format(end_time - start_time))
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats[:5]:
print (stat)
這兩個(gè)示例的執(zhí)行結(jié)果如下,可以放在一起進(jìn)行對(duì)比:
[dechin@dechin-manjaro yield]$ python3 square_sum.py
Time cost is: 0.24723434448242188s
square_sum.py:9: size=781 KiB, count=2, average=391 KiB
square_sum.py:12: size=24 B, count=1, average=24 B
square_sum.py:11: size=24 B, count=1, average=24 B
[dechin@dechin-manjaro yield]$ python3 yield_square_sum.py
Time cost is: 0.23023390769958496s
yield_square_sum.py:9: size=136 B, count=1, average=136 B
yield_square_sum.py:14: size=112 B, count=1, average=112 B
yield_square_sum.py:11: size=79 B, count=2, average=40 B
yield_square_sum.py:10: size=76 B, count=2, average=38 B
yield_square_sum.py:15: size=28 B, count=1, average=28 B
經(jīng)過比較我們發(fā)現(xiàn),兩種方法的計(jì)算時(shí)間是幾乎差不多的,但是在內(nèi)存占用上yield有著明顯的優(yōu)勢。當(dāng)然,也許這個(gè)例子并不是非常的恰當(dāng),但是本文主要還是介紹yield的使用方法及其應(yīng)用場景。
3.無限長迭代器
在參考鏈接1中提到了一種用法是無限長的迭代器,比如按順序返回所有的素?cái)?shù),那么此時(shí)我們?nèi)绻胷eturn來返回所有的元素并存儲(chǔ)到一個(gè)列表里面,就是一個(gè)非常不經(jīng)濟(jì)的辦法,所以可以使用yield來迭代生成,參考鏈接1中的源代碼如下所示:
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
那么類似的,這里我們用while True可以展示一個(gè)簡單的案例——返回所有的偶數(shù):
def yield_range2(i):
while True:
yield i
i += 2
#學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群:153708845
iter = yield_range2(0)
for i in range(10):
print (next(iter))
因?yàn)檫@里我們限制了長度是10,所以最終會(huì)返回10個(gè)偶數(shù):
[dechin@dechin-manjaro yield]$ python3 yield_iter.py
0
2
4
6
8
10
12
14
16
18
總結(jié)
本文介紹了python的迭代器yield,其實(shí)關(guān)于yield,我們可以簡單的將其理解為單個(gè)元素的return。文章來源:http://www.zghlxwxcb.cn/news/detail-861316.html
這樣不僅就初步理解了yield的使用語法,也能夠大概了解到y(tǒng)ield的優(yōu)勢,也就是在計(jì)算過程中每次只占用一個(gè)元素的內(nèi)存,而不需要一直存儲(chǔ)大量的元素在內(nèi)存中。文章來源地址http://www.zghlxwxcb.cn/news/detail-861316.html
到了這里,關(guān)于python使用迭代生成器yield減少內(nèi)存占用的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!