Skip to content

Commit d0be0bd

Browse files
committed
markdown source builds
Auto-generated via `{sandpaper}` Source : e00e48e Branch : main Author : Stacy Shcherbakova <[email protected]> Time : 2025-01-23 13:08:52 +0000 Message : Merge pull request #1 from ICR-RSE-Group/fix-issue-21 Fix issue 21 - reviewing the optimisation course
1 parent 863f549 commit d0be0bd

7 files changed

+54
-72
lines changed

md5sum.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
"config.yaml" "1a997dc990360662f7a0e4fba525bdf8" "site/built/config.yaml" "2025-01-14"
55
"index.md" "8f0476c27469136028995d6b7c9d4240" "site/built/index.md" "2025-01-14"
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2025-01-08"
7-
"episodes/optimisation-introduction.md" "79da398c049d3b22b45b132038c5c2a1" "site/built/optimisation-introduction.md" "2025-01-14"
8-
"episodes/optimisation-data-structures-algorithms.md" "ae2d5f70c4649800f35794b24c3cef2b" "site/built/optimisation-data-structures-algorithms.md" "2025-01-08"
9-
"episodes/optimisation-minimise-python.md" "37e3a2643171a7e2a432902b420929be" "site/built/optimisation-minimise-python.md" "2025-01-08"
7+
"episodes/optimisation-introduction.md" "d07ce4494a6f69c69a9cccaf10a8a6b9" "site/built/optimisation-introduction.md" "2025-01-23"
8+
"episodes/optimisation-data-structures-algorithms.md" "11cccec7f03a2ca8331e54f4a5a4adc7" "site/built/optimisation-data-structures-algorithms.md" "2025-01-23"
9+
"episodes/optimisation-minimise-python.md" "947d5c608432bc1c21d7b69f693254f8" "site/built/optimisation-minimise-python.md" "2025-01-23"
1010
"episodes/optimisation-use-latest.md" "5948276773890e97b7898292fddbcb39" "site/built/optimisation-use-latest.md" "2025-01-08"
1111
"episodes/optimisation-memory.md" "d819f88003ffd721bc6b1ec783a23390" "site/built/optimisation-memory.md" "2025-01-08"
1212
"episodes/optimisation-conclusion.md" "567478d44c721cbf1bc8a71297a54a56" "site/built/optimisation-conclusion.md" "2025-01-08"
1313
"episodes/long-break1.md" "dea66ed9de52386eebf67722b167a2a8" "site/built/long-break1.md" "2025-01-14"
14-
"episodes/profiling-introduction.md" "567d77d9a2dd68f36a0ccf024b15327f" "site/built/profiling-introduction.md" "2025-01-14"
15-
"episodes/profiling-functions.md" "94be5829062d7d0691bf192de13708ad" "site/built/profiling-functions.md" "2025-01-08"
16-
"episodes/profiling-lines.md" "13095a698cf9c4c6ee5fe4bdb653c103" "site/built/profiling-lines.md" "2025-01-08"
14+
"episodes/profiling-introduction.md" "e9fe7f86f9704b3e3655b55c0097ed67" "site/built/profiling-introduction.md" "2025-01-23"
15+
"episodes/profiling-functions.md" "53c79bfa7df29d91f8801d8a89e91772" "site/built/profiling-functions.md" "2025-01-23"
16+
"episodes/profiling-lines.md" "61538f73bc04694e8d5339c47c4d84f4" "site/built/profiling-lines.md" "2025-01-23"
1717
"episodes/profiling-conclusion.md" "b5687e26387b353ef23c6292f295ca02" "site/built/profiling-conclusion.md" "2025-01-08"
1818
"instructors/instructor-notes.md" "cae72b6712578d74a49fea7513099f8c" "site/built/instructor-notes.md" "2025-01-08"
1919
"learners/setup.md" "7de990fb6fba50d9aaafe092696b8168" "site/built/setup.md" "2025-01-14"

optimisation-data-structures-algorithms.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CPython for example uses [`newsize + (newsize >> 3) + 6`](https://github.com/pyt
6565

6666
This has two implications:
6767

68-
* If you are creating large static lists, they will use upto 12.5% excess memory.
68+
* If you are creating large static lists, they will use up to 12.5% excess memory.
6969
* If you are growing a list with `append()`, there will be large amounts of redundant allocations and copies as the list grows.
7070

7171
### List Comprehension
@@ -165,7 +165,7 @@ To retrieve or check for the existence of a key within a hashing data structure,
165165

166166
### Keys
167167

168-
Keys will typically be a core Python type such as a number or string. However multiple of these can be combined as a Tuple to form a compound key, or a custom class can be used if the methods `__hash__()` and `__eq__()` have been implemented.
168+
Keys will typically be a core Python type such as a number or string. However, multiple of these can be combined as a Tuple to form a compound key, or a custom class can be used if the methods `__hash__()` and `__eq__()` have been implemented.
169169

170170
You can implement `__hash__()` by utilising the ability for Python to hash tuples, avoiding the need to implement a bespoke hash function.
171171

@@ -265,7 +265,7 @@ Constructing a set with a loop and `add()` (equivalent to a list's `append()`) c
265265

266266
The naive list approach is 2200x times slower than the fastest approach, because of how many times the list is searched. This gap will only grow as the number of items increases.
267267

268-
Sorting the input list reduces the cost of searching the output list significantly, however it is still 8x slower than the fastest approach. In part because around half of it's runtime is now spent sorting the list.
268+
Sorting the input list reduces the cost of searching the output list significantly, however it is still 8x slower than the fastest approach. In part because around half of its runtime is now spent sorting the list.
269269

270270
```output
271271
uniqueSet: 0.30ms
@@ -280,9 +280,9 @@ uniqueListSort: 2.67ms
280280

281281
Independent of the performance to construct a unique set (as covered in the previous section), it's worth identifying the performance to search the data-structure to retrieve an item or check whether it exists.
282282

283-
The performance of a hashing data structure is subject to the load factor and number of collisions. An item that hashes with no collision can be checked almost directly, whereas one with collisions will probe until it finds the correct item or an empty slot. In the worst possible case, whereby all insert items have collided this would mean checking every single item. In practice, hashing data-structures are designed to minimise the chances of this happening and most items should be found or identified as missing with a single access.
283+
The performance of a hashing data structure is subject to the load factor and number of collisions. An item that hashes with no collision can be checked almost directly, whereas one with collisions will probe until it finds the correct item or an empty slot. In the worst possible case, whereby all insert items have collided this would mean checking every single item. In practice, hashing data-structures are designed to minimise the chances of this happening and most items should be found or identified as missing with single access.
284284

285-
In contrast if searching a list or array, the default approach is to start at the first item and check all subsequent items until the correct item has been found. If the correct item is not present, this will require the entire list to be checked. Therefore the worst-case is similar to that of the hashing data-structure, however it is guaranteed in cases where the item is missing. Similarly, on-average we would expect an item to be found half way through the list, meaning that an average search will require checking half of the items.
285+
In contrast, if searching a list or array, the default approach is to start at the first item and check all subsequent items until the correct item has been found. If the correct item is not present, this will require the entire list to be checked. Therefore, the worst-case is similar to that of the hashing data-structure, however it is guaranteed in cases where the item is missing. Similarly, on-average we would expect an item to be found halfway through the list, meaning that an average search will require checking half of the items.
286286

287287
If however the list or array is sorted, a binary search can be used. A binary search divides the list in half and checks which half the target item would be found in, this continues recursively until the search is exhausted whereby the item should be found or dismissed. This is significantly faster than performing a linear search of the list, checking a total of `log N` items every time.
288288

@@ -333,9 +333,7 @@ print(f"linear_search_list: {timeit(linear_search_list, number=repeats)-gen_time
333333
print(f"binary_search_list: {timeit(binary_search_list, number=repeats)-gen_time:.2f}ms")
334334
```
335335

336-
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-
The linear search of the list is more than 56,600x slower than the fastest, it really shouldn't be used!
336+
Searching the set is the fastest, performing 25,000 searches in 0.04ms. 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. The linear search of the list is more than 56,600x slower than searching the set, it really shouldn't be used!
339337

340338
```output
341339
search_set: 0.04ms

optimisation-introduction.md

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ exercises: 0
1919
## Introduction
2020

2121
<!-- Changing the narrative: you'd better learn how to write a good code an what are the good practice -->
22-
Think about optimization as the first step on your journey to writing high-performance code.
22+
Think about optimisation as the first step on your journey to writing high-performance code.
2323
It’s like a race: the faster you can go without taking unnecessary detours, the better.
2424
Code optmisation is all about understanding the principles of efficiency in Python and being conscious of how small changes can yield massive improvements.
2525

2626
<!-- Necessary to understand how code executes (to a degree) -->
27-
These are the first steps in code optimization: making better choices as you write your code and have an understanding of what a computer is doing to execute it.
27+
These are the first steps in code optimisation: making better choices as you write your code and have an understanding of what a computer is doing to execute it.
2828

2929
<!-- Goal is to give you a high level understanding of how your code executes. You don't need to be an expert, even a vague general understanding will leave you in a stronger position. -->
3030
A high-level understanding of how your code executes, such as how Python and the most common data-structures and algorithms are implemented, can help you identify suboptimal approaches when programming. If you have learned to write code informally out of necessity, to get something to work, it's not uncommon to have collected some bad habits along the way.
@@ -34,41 +34,25 @@ The remaining content is often abstract knowledge, that is transferable to the v
3434

3535
## Optimising code from scratch: trade-off between performance and maintainability
3636

37-
> Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: **premature optimization is the root of all evil**. Yet we should not pass up our opportunities in that critical 3%. - Donald Knuth
37+
> Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: **premature optimisation is the root of all evil**. Yet we should not pass up our opportunities in that critical 3%. - Donald Knuth
3838
39-
This classic quote among computer scientists states; when considering optimisation it is important to focus on the potential impact, both to the performance and maintainability of the code.
40-
41-
Advanced optimisations, mostly outside the scope of this course, can increase the cost of maintenance by obfuscating what code is doing. Even if you are a solo-developer working on private code, your future self should be able to easily comprehend your implementation.
42-
43-
Therefore, the balance between the impact to both performance and maintainability should be considered when optimising code.
39+
This classic quote among computer scientists states; when considering optimisation it is important to focus on the potential impact, both to the performance and maintainability of the code. Advanced optimisations, mostly outside the scope of this course, can increase the cost of maintenance by obfuscating what code is doing. Even if you are a solo-developer working on private code, your future self should be able to easily comprehend your implementation. Therefore, the balance between the impact to both performance and maintainability should be considered when optimising code.
4440

4541
This is not to say, don't consider performance when first writing code. The selection of appropriate algorithms and data-structures covered in this course form a good practice, simply don't fret over a need to micro-optimise every small component of the code that you write.
4642

47-
4843
## Ensuring Reproducible Results when optimising an existing code
4944

5045
<!-- This is also good practice when optimising your code, to ensure mistakes aren't made -->
5146
When optimising an existing code, you are making speculative changes. It's easy to make mistakes, many of which can be subtle. Therefore, it's important to have a strategy in place to check that the outputs remain correct.
5247

53-
Testing is hopefully already a seamless part of your research software development process.
54-
Test can be used to clarify how your software should perform, ensuring that new features work as intended and protecting against unintended changes to old functionality.
55-
56-
There are a plethora of methods for testing code.
48+
Testing is hopefully already a seamless part of your research software development process. Test can be used to clarify how your software should perform, ensuring that new features work as intended and protecting against unintended changes to old functionality.
5749

5850
## pytest Overview
5951

60-
Most Python developers use the testing package [pytest](https://docs.pytest.org/en/latest/), it's a great place to get started if you're new to testing code.
52+
There are a plethora of methods for testing code. Most Python developers use the testing package [pytest](https://docs.pytest.org/en/latest/), it's a great place to get started if you're new to testing code. Tests should be created within a project's testing directory, by creating files named with the form `test_*.py` or `*_test.py`. pytest looks for these files, when running the test suite. Within the created test file, any functions named in the form `test*` are considered tests that will be executed by pytest. The `assert` keyword is used, to test whether a condition evaluates to `True`.
6153

6254
Here's a quick example of how a test can be used to check your function's output against an expected value.
6355

64-
Tests should be created within a project's testing directory, by creating files named with the form `test_*.py` or `*_test.py`.
65-
66-
pytest looks for these files, when running the test suite.
67-
68-
Within the created test file, any functions named in the form `test*` are considered tests that will be executed by pytest.
69-
70-
The `assert` keyword is used, to test whether a condition evaluates to `True`.
71-
7256
```python
7357
# file: test_demonstration.py
7458

0 commit comments

Comments
 (0)