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-minimise-python.md
+7-8Lines changed: 7 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,17 +20,16 @@ exercises: 0
20
20
21
21
::::::::::::::::::::::::::::::::::::::::::::::::
22
22
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/ -->
24
24
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.
26
26
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.
28
28
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.
0 commit comments