Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added episodes/fig/amdahls_law.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions episodes/fig/amdahls_law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Construct a graph demonstrating Amdahl's law

"""
import matplotlib.pyplot as plt

def calc_overall_speedup(acute_proportion, acute_speedup):
original_time = 1
unaffected_time = (1-acute_proportion)*original_time
affected_time = (acute_proportion*original_time)/acute_speedup
overall_time = unaffected_time + affected_time
overall_speedup = original_time / overall_time
return overall_speedup

plt.rcParams['text.usetex'] = True
# How much the acute piece of code is optimised
acute_speedup = [2 ** i for i in range(17)]

# 1 plot for each "proportion" of the runtime taken up by the acute piece of code to be optimised
for proportion in [0.95, 0.9, 0.75, 0.5, 0.01]:
overall_speedup = [calc_overall_speedup(proportion, i) for i in acute_speedup]
plt.plot(acute_speedup, overall_speedup, label=f"{proportion*100}%")

plt.xlabel("$S$")
plt.ylabel("$S_{overall}$")
#plt.ticklabel_format(style='plain')
plt.xscale('symlog')
plt.minorticks_off()
plt.xticks(acute_speedup, acute_speedup, rotation=90)
plt.xlim(xmin=1)
plt.yticks([i for i in range(2, 22, 2)], [str(i) for i in range(2, 22, 2)])
plt.ylim(ymin=0)
plt.legend(title = "$P$")
plt.tight_layout()
plt.savefig('amdahls_law.png')
plt.show()
26 changes: 25 additions & 1 deletion episodes/optimisation-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,31 @@ Therefore, the balance between the impact to both performance and maintainabilit

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 good practice, simply don't fret over a need to micro-optimise every small component of the code that you write.

### Performance of Python
::::::::::::::::::::::::::::::::::::: callout

### Amdahl's Law

> the overall performance improvement gained by optimising a single part of a system is limited by the fraction of time that the improved part is actually used - Gene Amdahl

The maximum possible speedup to a system can be calculated with the equation

$S_{overall} = \frac{1}{(1-P) + \frac{P}{S}}$

where:

* $P$ is the original proportion of execution time spent in the optimisable section.
* $S$ is the speedup to the optimisable section.
* $S_{overall}$ is the total system speedup.

This is demonstrated in the below graph.

![A graph demonstrating the impact of Amdahl's Law.](episodes/fig/amdahls_law.png){alt="A graph containing 5 plots. The x-axis is labelled 'S' with ticks on a log scale from 1 to 65536. The y-axis is labelled 'T' with ticks from 2 to 20. The 5 plots are labelled 'P' marked 1%, 50%, 75%, 90% and 95%. The 1% plot is a flat line with overall speedup close to 1x, The 50% plot quickly plateaus at 2x overall speedup. 75% plateaus at 4x, 90% plateaus at 10x and 95% at 20x."}

It's clear that the maximum possible speedup to a system, quickly plateaus according to the proportion of execution time taken up by the section of code to be optimised. If the section of code to be optimised only occupies 50% of the system runtime, even if it's sped up 65,000x, the impact to the overall system performance is only a 2x speedup. In contrast, if the section to be optimised occupies 95% of the runtime the maximum impact is a 20x speedup!

:::::::::::::::::::::::::::::::::::::::::::::

## Performance of Python

If you've read about different programming languages, you may have heard that there’s a difference between “interpreted” languages (like Python) and "compiled" languages (like C). You may have heard that Python is slow *because* it is an interpreted language.
To understand where this comes from (and how to get around it), let's talk a little bit about how Python works.
Expand Down
2 changes: 1 addition & 1 deletion learners/ppp.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ The 25 minute talk, which introduces many of the Python patterns from the Optimi

The talk has an accompanying Jupyter notebook, allowing people to benchmark the patterns on their own hardware.

The notebook can be downloaded [here](https://drive.google.com/file/d/1YTEXJlDaaZBAGo-Aodukuw0ArVJakdBB/view?usp=sharing).
[Download the notebook here!](https://drive.google.com/file/d/1YTEXJlDaaZBAGo-Aodukuw0ArVJakdBB/view?usp=sharing).