From charlesreid1

(Created page with "==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 l...")
 
Line 12: Line 12:
   data.append(None)
   data.append(None)
</pre>
</pre>
===Plot of list sizeof===
[[Image:ListSizeof.png|500px]]

Revision as of 03:17, 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