Skip to content

Instantly share code, notes, and snippets.

@lvsl-deactivated
Created June 28, 2014 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvsl-deactivated/000eccf3d66540d75def to your computer and use it in GitHub Desktop.
Save lvsl-deactivated/000eccf3d66540d75def to your computer and use it in GitHub Desktop.
array.array VS list
import array
import tracemalloc
from contextlib import contextmanager
@contextmanager
def tracemem(msg):
tracemalloc.start()
snapshot1 = tracemalloc.take_snapshot()
yield
snapshot2 = tracemalloc.take_snapshot()
print(snapshot2.compare_to(snapshot1, 'lineno')[0])
def main():
with tracemem('array'):
a = array.array('Q', range(10000000))
del a
with tracemem('list'):
l = list(range(10000000))
del l
if __name__ == "__main__":
'''
Om my machine:
python3 -X tracemalloc test_allocations.py
test_allocations.py:16: size=78.1 MiB (+78.1 MiB), count=3 (+3), average=26.0 MiB
test_allocations.py:20: size=353 MiB (+353 MiB), count=9999745 (+9999745), average=37 B
'''
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment