One Minute Python Tricks (2) Who Is More Efficiency? Iterator vs Generator

list_1 = [x for x in range(10000000)]
print(sum(list_1))
list_2 = (x for x in range(10000000))
print(sum(list_2))

Memory Efficiency

import os
import psutil
import time

def show_memory_info(hint):
pid = os.getpid()
p = psutil.Process(pid)

info = p.memory_full_info()
memory = info.uss / 1024 / 1024
print(f'{hint} memory used: {memory} MB')


def test_iterator():
show_memory_info('Initing iterator')
list_1 = [x for x in range(10000000)]
show_memory_info('After iterator initiated')
print(sum(list_1))
show_memory_info('After sum is called')


def test_generator():
show_memory_info('Initing generator')
list_2 = (x for x in range(10000000))
show_memory_info('After generator initiated')
print(sum(list_2))
show_memory_info('After sum is called')


if __name__ == '__main__':
start = time.perf_counter()
test_iterator()
end = time.perf_counter()
print(f'Iterator used {end - start} seconds')
print(f'='*40)
start = time.perf_counter()
test_generator()
end = time.perf_counter()
print(f'Generator used {end - start} seconds')
Initing iterator memory used: 6.3125 MB
After iterator initiated memory used: 403.5 MB
49999995000000
After sum is called memory used: 403.5 MB
Iterator used 0.759843149 seconds
========================================
Initing generator memory used: 21.3359375 MB
After generator initiated memory used: 21.3359375 MB
49999995000000
After sum is called memory used: 21.3359375 MB
Generator used 0.5480666000000001 seconds

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store