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)
Obviously, generator can boost the performance by saving your memory. It's not holding all of the results in memory. But once you use the list() function, you'll lose this advantage.
网友评论