Created
August 29, 2021 02:22
-
-
Save geniusnhu/45bd93f245af1e895830ac2fc0db2a12 to your computer and use it in GitHub Desktop.
List vs Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import sys | |
>>> my_generator_list = (i for i in range(100000)) | |
>>> print(f"My generator is {sys.getsizeof(my_generator_list)} bytes") | |
My generator is 128 bytes | |
>>> timeit(my_generator_list) | |
10000000 loops, best of 5: 32 ns per loop | |
>>> my_list = [i for i in range(100000)] | |
>>> print(f"My list is {sys.getsizeof(my_list)} bytes") | |
My list is 824472 bytes | |
>>> timeit(my_list) | |
10000000 loops, best of 5: 34.5 ns per loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment