From charlesreid1

No edit summary
Line 18: Line 18:


[[Image:ListSizeofLonger.png|500px]]
[[Image:ListSizeofLonger.png|500px]]
{{DataStructuresFlag}}

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)

Plot of list sizeof

ListSizeof.png

ListSizeofLonger.png