Skip to content

Instantly share code, notes, and snippets.

@matheus-rossi
Created April 8, 2024 18:31
Show Gist options
  • Save matheus-rossi/2a60d8b9dcb8a25b387a78a3e3bfc0aa to your computer and use it in GitHub Desktop.
Save matheus-rossi/2a60d8b9dcb8a25b387a78a3e3bfc0aa to your computer and use it in GitHub Desktop.
import sys
# List comprehension
list_comprehension = [i for i in range(10_000_000)]
print(f"List comprehension memory: {sys.getsizeof(list_comprehension) / (1024 * 1024)} MB")
# Yield generator
def generator():
for i in range(10_000_000):
yield i
gen = generator()
print(f"Generator memory: {sys.getsizeof(gen) / (1024 * 1024)} MB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment