Created
October 12, 2021 11:37
-
-
Save e-io/9869858ee7c44edeed32a838f874aea1 to your computer and use it in GitHub Desktop.
How many elements of memory are reserved for python list (in standard CPython) by number of items.
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
list_size = 0 | |
# memory_size means total memory for this list whithout header memory | |
memory_size = 0 | |
last_memory_size = 0 | |
# if one element takes eight bytes of memory total | |
# then list with 2**20 elements weights 8 megabytes | |
# (it should be true at least for list with several kinds of elements, like True/False or 0/.../9) | |
max_output = 2**20 | |
output1 = 'Visible elements:' | |
output2 = 'Total elements:' | |
print(f"{output1} - elements in python's list, which we can use in code") | |
print(f"{output2} - elements of memory, which are reserved for this list") | |
print(f"*this data is actual for the most popular realisation of python - CPython.)") | |
width1 = len(output1) | |
width2 = len(output2) | |
print(f'{{: <{width1}}} | {{: <{width2}}}'.format(output1, output2)) | |
# ns - number system. d for decimal, x for hexadecimal, o for octal, b for binary | |
ns = 'd' | |
while True: | |
print(f'{{: >{width1}}} | {{: >{width2}_{ns}}}'.format(f'{list_size:_{ns}} - {memory_size:_{ns}}', memory_size)) | |
if memory_size >= max_output: | |
break | |
list_size = memory_size + 1 | |
if list_size <= 8: | |
memory_size = list_size + 3 | |
else: | |
memory_size = list_size + list_size // 8 + 6 | |
# This addiction is similar to the Fibonacci series | |
# Эта зависимость похожа на ряд Фибоначчи |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment