import time, sys
genstartime = time.perf_counter()
gensquares = (x**2 for x in range(500000))
# print(time.perf_counter() - genstartime, type(gensquares))
gencompstartime = time.perf_counter()
finalgenlist = [square for square in gensquares]
print(
"Generator with list comprehension. Generator time:",
time.perf_counter() - gencompstartime,
"Final time:",
time.perf_counter() - genstartime,
)
genstartime = time.perf_counter()
gensquares = (x**2 for x in range(500000))
# print(time.perf_counter() - genstartime, type(gensquares))
gencompstartime = time.perf_counter()
finalgenlist = list(gensquares)
print(
"Generator with list(). Generator time:",
time.perf_counter() - gencompstartime,
"Final time:",
time.perf_counter() - genstartime,
)
print('Memory size of generator in bytes:', sys.getsizeof(gensquares),
'Memory size of list in bytes:', sys.getsizeof(finalgenlist))
# An important thing to note is that generators iterate over an object lazily,
# meaning they do not store their contents in memory.
# listfromgen = list(gensquares)
# print(listfromgen, len(listfromgen), sys.getsizeof(listfromgen)) # [] 0 56
# print(len(finalgenlist)) # list kept in memory, len equal to range
liststartime = time.perf_counter()
listsquares = [x**2 for x in range(500000)]
print(
"Simple list comprehension", time.perf_counter() - liststartime, type(listsquares)
)
# print( 'Memory size after list comprehension in bytes:', sys.getsizeof(listsquares))
Results with Python 3.11.4:
Generator with list comprehension. Generator time: 0.09394780000002356 Final time: 0.09395289999883971
Generator with list(). Generator time: 0.0871331000016653 Final time: 0.08714600000166683
Memory size of generator in bytes: 208 Memory size of list in bytes: 4167352
Simple list comprehension 0.06546270000035292 <class 'list'>
Generator with list comprehension. Generator time: 0.08868479999728152 Final time: 0.08870200000092154
Generator with list(). Generator time: 0.08478610000020126 Final time: 0.08480150000104913
Memory size of generator in bytes: 208 Memory size of list in bytes: 4167352
Simple list comprehension 0.06996629999775905 <class 'list'>
Generator with list comprehension. Generator time: 0.08237219999864465 Final time: 0.08237809999991441
Generator with list(). Generator time: 0.09145460000218009 Final time: 0.09146870000040508
Memory size of generator in bytes: 208 Memory size of list in bytes: 4167352
Simple list comprehension 0.0625220999972953 <class 'list'>
Generator with list(). Generator time: 0.0871331000016653 Final time: 0.08714600000166683
Memory size of generator in bytes: 208 Memory size of list in bytes: 4167352
Simple list comprehension 0.06546270000035292 <class 'list'>
Generator with list comprehension. Generator time: 0.08868479999728152 Final time: 0.08870200000092154
Generator with list(). Generator time: 0.08478610000020126 Final time: 0.08480150000104913
Memory size of generator in bytes: 208 Memory size of list in bytes: 4167352
Simple list comprehension 0.06996629999775905 <class 'list'>
Generator with list comprehension. Generator time: 0.08237219999864465 Final time: 0.08237809999991441
Generator with list(). Generator time: 0.09145460000218009 Final time: 0.09146870000040508
Memory size of generator in bytes: 208 Memory size of list in bytes: 4167352
Simple list comprehension 0.0625220999972953 <class 'list'>