yield vs return
2018-04-02 本文已影响0人
whenitsallover
import memory_profiler
import time
start = time.time()
print("Before {}Mb".format(memory_profiler.memory_usage()))
def calculation(para):
result = []
for i in para:
result.append(i)
return result
'''
return [52.0] Mb [56.25390625]Mb 1.644s
yield [51.86328125]Mb [51.87109375]Mb 0.202s
'''
res = calculation(range(10000000))
print("After {}Mb".format(memory_profiler.memory_usage()))
print(time.time()-start)