Skip to content

Commit ee341e0

Browse files
committed
add a brief definition of CPython and simplify text
1 parent eb289fa commit ee341e0

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

episodes/optimisation-data-structures-algorithms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ In CPython's [dictionary](https://github.com/python/cpython/blob/main/Objects/di
157157

158158

159159
When a dictionary or hash table in Python grows, the underlying storage is resized, which necessitates re-inserting every existing item into the new structure. This process can be computationally expensive but is essential for maintaining efficient average probe times when searching for keys.
160-
![An visual explanation of linear probing, CPython uses an advanced form of this.](episodes/fig/hash_linear_probing.png){alt="A iagram showing how keys (hashes) 37, 64, 14, 94, 67 are inserted into a hash table with 11 indices. The insertion of 59, 80, and 39 demonstrates linear probing to resolve collisions."}
160+
![A visual explanation of linear probing, CPython uses an advanced form of this.](episodes/fig/hash_linear_probing.png){alt="A diagram showing how keys (hashes) 37, 64, 14, 94, 67 are inserted into a hash table with 11 indices. The insertion of 59, 80, and 39 demonstrates linear probing to resolve collisions."}
161161
To look up or verify the existence of a key in a hashing data structure, the key is re-hashed, and the process mirrors that of insertion. The corresponding index is probed to see if it contains the provided key. If the key at the index matches, the operation succeeds. If an empty index is reached before finding the key, it indicates that the key does not exist in the structure.
162162

163163
The above diagrams shows a hash table of 5 elements within a block of 11 slots:

episodes/optimisation-minimise-python.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ exercises: 0
2020

2121
::::::::::::::::::::::::::::::::::::::::::::::::
2222

23-
Python is an interpreted programming language. When you execute your `.py` file, the (default) CPython back-end compiles your Python source code to an intermediate bytecode. This bytecode is then interpreted in software at runtime generating instructions for the processor as necessary. This interpretation stage, and other features of the language, harm the performance of Python (whilst improving its usability).<!-- https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/ -->
23+
Python is an interpreted language. When you run a .py file, the CPython interpreter first converts the Python code into bytecode. CPython is the default implementation of Python, written in C. This bytecode is then processed at runtime to generate instructions for the CPU. While this makes Python easier to use, it can slow down performance.<!-- https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/ -->
2424

25-
In comparison, many languages such as C/C++ compile directly to machine code. This allows the compiler to perform low-level optimisations that better exploit hardware nuance to achieve fast performance. This however comes at the cost of compiled software not being cross-platform.
25+
In contrast, languages like C/C++ are compiled directly into machine code, allowing the compiler to optimize for better performance. However, this means C/C++ programs aren't as easily portable across different platforms.
2626

27-
Whilst Python will rarely be as fast as compiled languages like C/C++, it is possible to take advantage of the CPython back-end and packages such as NumPy and Pandas that have been written in compiled languages to expose this performance.
27+
Although Python isn’t as fast as languages like C/C++, it can still be efficient by using tools like NumPy and Pandas, which are written in faster compiled languages.
2828

29-
A simple example of this would be to perform a linear search of a list (in the previous episode we did say this is not recommended).
30-
The below example creates a list of 2500 integers in the inclusive-exclusive range `[0, 5000)`.
31-
It then searches for all the even numbers in that range.
32-
`searchlistPython()` is implemented manually, iterating `ls` checking each individual item in Python code.
33-
`searchListC()` in contrast uses the `in` operator to perform each search, which allows CPython to implement the inner loop in it's C back-end.
29+
30+
A simple example of this is performing a linear search on a list (though we mentioned in the previous episode that this isn’t the most efficient approach). In the following example, we create a list of 2500 integers in the range `[0, 5000)`. The goal is to search for all even numbers within that range.
31+
32+
The function `searchlistPython()` manually iterates through the list (`ls`) and checks each item using Python code. On the other hand, `searchListC()` uses the `in` operator, which lets CPython handle the search more efficiently by running the inner loop in its C back-end.
3433

3534
```python
3635
import random

0 commit comments

Comments
 (0)