Arrays/Python/Sizeof
From charlesreid1
Contents
Goodrich Python Chapter 5
R-1 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
R-2 Only printing the sizes where the array is exhausted and resized
import sys n = 10000 data = [] so = sys.getsizeof(data) for k in range(n): sonew = sys.getsizeof(data) if(sonew != so): so = sonew mylen = len(data) print("len {0}, sizeof (bytes) {1}".format(sonew,mylen)) data.append(None)
and the output:
$ python listsize-exhausted.py len 104, sizeof (bytes) 1 len 136, sizeof (bytes) 5 len 200, sizeof (bytes) 9 len 272, sizeof (bytes) 17 len 352, sizeof (bytes) 26 len 440, sizeof (bytes) 36 len 536, sizeof (bytes) 47 len 648, sizeof (bytes) 59 len 776, sizeof (bytes) 73 len 920, sizeof (bytes) 89 len 1080, sizeof (bytes) 107 len 1256, sizeof (bytes) 127 len 1456, sizeof (bytes) 149 len 1680, sizeof (bytes) 174 len 1936, sizeof (bytes) 202 len 2224, sizeof (bytes) 234 len 2544, sizeof (bytes) 270 len 2904, sizeof (bytes) 310 len 3312, sizeof (bytes) 355 len 3768, sizeof (bytes) 406 len 4280, sizeof (bytes) 463 len 4856, sizeof (bytes) 527 len 5504, sizeof (bytes) 599 len 6240, sizeof (bytes) 680 len 7064, sizeof (bytes) 772 len 7992, sizeof (bytes) 875 len 9032, sizeof (bytes) 991 len 10208, sizeof (bytes) 1121 len 11528, sizeof (bytes) 1268 len 13016, sizeof (bytes) 1433 len 14688, sizeof (bytes) 1619 len 16568, sizeof (bytes) 1828 len 18680, sizeof (bytes) 2063 len 21056, sizeof (bytes) 2327 len 23736, sizeof (bytes) 2624 len 26744, sizeof (bytes) 2959 len 30128, sizeof (bytes) 3335 len 33936, sizeof (bytes) 3758 len 38224, sizeof (bytes) 4234 len 43048, sizeof (bytes) 4770 len 48472, sizeof (bytes) 5373 len 54576, sizeof (bytes) 6051 len 61440, sizeof (bytes) 6814 len 69168, sizeof (bytes) 7672 len 77856, sizeof (bytes) 8638 len 87632, sizeof (bytes) 9724
R-3 showing dynamic shrinkage
Arrays automatically double in size as more items are added to them, but they also shrink when items are removed from them.
This is accomplished by specifying a sequence of add/remove operation sizes:
import sys from matplotlib.pyplot import * import seaborn as sns data = [] sequence = [1000,-800,200,-200,1000] tot = sum(sequence) so = sys.getsizeof(data) x = [0]*tot y = [0]*tot for n in sequence: if(n>0): print("++++ADDING ITEMS++++") else: print("----REMOVING ITEMS----") for k in range(abs(n)): sonew = sys.getsizeof(data) mylen = len(data) x.append(sonew) y.append(mylen) if(sonew != so): so = sonew print("len {0}, sizeof (bytes) {1}".format(sonew,mylen)) if(n>0): data.append(None) else: data.pop() xlabel('Array size') ylabel('Byte size') title('Size in bytes vs. List size') i = 0 for n,w in zip(sequence,[abs(j) for j in sequence]): plot(x[i:i+w],y[i:i+w],'-o',label=str(n)) i += w legend() savefig('listsize-pop.png') show()
Data Structures Part of Computer Science Notes
This is the staging ground for computer science notes as part of the 2017 CS Study Plan.
Classes of data structures: Abstract Data Types Array-based and Link-based memory management: ArrayLists and Linked Lists Algorithmic Analysis of Data Structures: Algorithmic Analysis of Data Structures Advanced data structures: Advanced Data Structures
|
Arrays Part of Computer Science Notes
Series on Data Structures Python: Arrays/Python · Arrays/Python/Sizeof · Arrays/Python/AppendCost · Arrays/Python/CaesarCipher · Arrays/Python/CompactArrays · Arrays/Python/DynamicArray Java: Arrays/Java · Arrays/Java/CaesarCipher · Arrays/Java/FisherYates · Arrays/Java/PythonList · Arrays/Java/Repeatedly_Remove Categories: Category:Python Arrays
|
Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|
Priority Queues and Heaps Part of Computer Science Notes
Series on Data Structures
Java: Priority Queues/Java · Priority Queues/ADT · Priority Queues/Sorted · Priority Queues/Unsorted Performance: Priority Queues/Timing and Performance Applications: Maximum Oriented Priority Queue · Priority Queues/Stack
Priority Queues/Heap · Priority Queues/Java · Priority Queues/Comparators
|
Linked List Part of Computer Science Notes
Series on Data Structures Java: Linked Lists/Java · Linked Lists/Java/Single · Linked Lists/Java/Double · Linked Lists/Java/Circular Performance: Linked Lists/Java/Timing · Linked Lists/Java/Reverse Python: Linked Lists/Python · Linked Lists/Python/Single
|
Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|
Search Trees Part of Computer Science Notes
Series on Data Structures
Binary Search Trees · Balanced Search Trees Trees/OOP · Search Trees/OOP · Tree Traversal/OOP · Binary Trees/Inorder
(Note that heaps are also value-sorting trees with minimums at the top. See Template:PriorityQueuesFlag and Priority Queues.)
|
Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|