6/39
Part 1 of 7 · Nodes and costsLearn
Each node is a whole object: 48 to 80 bytes, and slower to walk
Every node costs memory, and every step costs time.
list
8.0
deque
8.3
Node, __slots__
48.0
Node
80.0
That's measured with tracemalloc, Python's memory tracer. Every value is None, so only the structure counts.
A list stores one pointer per item. A linked list stores an object per item, and the object holds the pointer. __slots__ = ("val", "next") on the class drops the storage each object keeps for its attributes. It's a detail: the point is that a node is a whole Python object.
Walking costs more too. Adding up 1,000,000 values, median of 5 runs:
Adding up a million values: sum(xs) took 3 ms. About how long did walking the nodes take?
Pick an answer above to see the rest