From charlesreid1

Revision as of 03:19, 29 May 2017 by Admin (talk | contribs)

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)

Plot of list sizeof

ListSizeof.png

ListSizeofLonger.png