You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/optimisation-data-structures-algorithms.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,16 +65,17 @@ CPython for example uses [`newsize + (newsize >> 3) + 6`](https://github.com/pyt
65
65
66
66
This has two implications:
67
67
68
-
* If you are creating large static lists, they will use upto 12.5% excess memory.
69
68
* If you are growing a list with `append()`, there will be large amounts of redundant allocations and copies as the list grows.
69
+
* The resized list may use up to 12.5% excess memory.
70
+
<!-- This only applies when resizing a list. When creating a list of a particular size from scratch, CPython will not overallocate as much memory: https://github.com/python/cpython/blob/a571a2fd3fdaeafdfd71f3d80ed5a3b22b63d0f7/Objects/listobject.c#L101 -->
70
71
71
72
### List Comprehension
72
73
73
74
If creating a list via `append()` is undesirable, the natural alternative is to use list-comprehension.
74
75
75
76
List comprehension can be twice as fast at building lists than using `append()`.
76
77
This is primarily because list-comprehension allows Python to offload much of the computation into faster C code.
77
-
General python loops in contrast can be used for much more, so they remain in Python bytecode during computation which has additional overheads.
78
+
General Python loops in contrast can be used for much more, so they remain in Python bytecode during computation which has additional overheads.
78
79
79
80
This can be demonstrated with the below benchmark:
80
81
@@ -112,7 +113,7 @@ Results will vary between Python versions, hardware and list lengths. But in thi
112
113
113
114
## Tuples
114
115
115
-
In contrast, Python's tuples are immutable static arrays (similar to strings), their elements cannot be modified and they cannot be resized.
116
+
In contrast to lists, Python's tuples are immutable static arrays (similar to strings): Their elements cannot be modified and they cannot be resized.
116
117
117
118
Their potential use-cases are greatly reduced due to these two limitations, they are only suitable for groups of immutable properties.
118
119
@@ -160,7 +161,7 @@ When the hashing data structure exceeds a given load factor (e.g. 2/3 of indices
160
161
161
162
{alt="A diagram demonstrating how the keys (hashes) 37, 64, 14, 94, 67 are inserted into a hash table with 11 indices. This is followed by the insertion of 59, 80 and 39 which require linear probing to be inserted due to collisions."}
162
163
163
-
To retrieve or check for the existence of a key within a hashing data structure, the key is hashed again and a process equivalent to insertion is repeated. However, now the key at each index is checked for equality with the one provided. If any empty index is found before an equivalent key, then the key must not be present in the ata structure.
164
+
To retrieve or check for the existence of a key within a hashing data structure, the key is hashed again and a process equivalent to insertion is repeated. However, now the key at each index is checked for equality with the one provided. If any empty index is found before an equivalent key, then the key must not be present in the data structure.
Searching the set is fastest performing 25,000 searches in 0.04ms.
337
-
This is followed by the binary search of the (sorted) list which is 145x slower, although the list has been filtered for duplicates. A list still containing duplicates would be longer, leading to a more expensive search.
338
+
This is followed by the binary search of the (sorted) list which is 145x slower, although the list has been filtered for duplicates. A list still containing duplicates would be longer, leading to a more expensive search.
338
339
The linear search of the list is more than 56,600x slower than the fastest, it really shouldn't be used!
0 commit comments