Created
June 28, 2014 21:06
-
-
Save lvsl-deactivated/000eccf3d66540d75def to your computer and use it in GitHub Desktop.
array.array VS list
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 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