Arrays/Python/Sizeof: Difference between revisions
From charlesreid1
| Line 16: | Line 16: | ||
[[Image:ListSizeof.png|500px]] | [[Image:ListSizeof.png|500px]] | ||
[[Image:ListSizeofLonger.png|500px]] | |||
Revision as of 03:19, 29 May 2017
Measuring size in bytes of list
This script illustrates how a list grows dynamically, in proportion to its size, when we add additional items to the list. It doubles in length each time it is resized, for an O(n) operation. If it increased in size by a constant amount, that would impose a bottleneck and be an O(n^2) operation.
import sys
data = []
for k in range(n):
a = len(data)
b = sys.getsizeof(data)
print("Length: {0:3d}\tSize in bytes: {1:4d}".format(a,b))
data.append(None)