Skip to content

Commit

Permalink
Optimisation Episodes Draft
Browse files Browse the repository at this point in the history
Still requires internal review and additional exercises/flow changes.
  • Loading branch information
Robadob authored Jan 29, 2024
1 parent 6e976a3 commit 8a31cd6
Show file tree
Hide file tree
Showing 21 changed files with 3,908 additions and 132 deletions.
9 changes: 8 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ contact: '[email protected]'

# Order of episodes in your lesson
episodes:
- introduction.md
- profiling-introduction.md
- profiling-functions.md
- profiling-lines.md
- profiling-conclusion.md
- optimisation-introduction.md
- optimisation-use-latest.md
- optimisation-memory.md
- optimisation-list-tuple.md
- optimisation-dict-set.md
- optimisation-minimise-python.md
- optimisation-conclusion.md

# Information for Learners
learners:
- setup.md

# Information for Instructors
instructors:
Expand Down
Binary file added episodes/fig/cpython_list_allocations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions episodes/fig/cpython_list_allocations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Construct a graph demonstrating the number of re-allocations
if growing a CPython List with append()
"""
t = 0
a = 0
x = range(1000000)

allocs = [0 for x in range(1000000)]
while t < 1000000:
t += 1
t_old = t
# https://github.com/python/cpython/blob/a571a2fd3fdaeafdfd71f3d80ed5a3b22b63d0f7/Objects/listobject.c#L74
t = t + (t >> 3) + 6
a+=1
for k in range(t_old, t+1):
if k < len(allocs):
allocs[k] = a

import matplotlib.pyplot as plt

plt.plot(x, allocs)

plt.xlabel("Appends")
plt.ylabel("Resizes")
#plt.ticklabel_format(style='plain')
#plt.xscale('symlog')
plt.savefig('cpython_list_allocations.png')
plt.show()
2,341 changes: 2,341 additions & 0 deletions episodes/fig/hash_linear_probe.ai

Large diffs are not rendered by default.

Binary file added episodes/fig/hash_linear_probing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/latency.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions episodes/fig/latency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Construct a graph demonstrating the difference in latencies relevant to code execution
Inspired by
https://gist.github.com/jboner/2841832
Using data from
https://www.intel.com/content/www/us/en/developer/articles/technical/memory-performance-in-a-nutshell.html
"""
import matplotlib.pyplot as plt
label = ["L1 cache", "L2 cache", "L3 cache", "RAM", "SSD", "HDD", "Ldn->Ca->Ldn"]
latency_ns = [1, 4, 40, 80, 8000, 80000, 140000000]

plt.figure().set_figheight(2)
plt.barh(label, latency_ns)

plt.xlabel("Latency (nanoseconds)")
#plt.ylabel("")
plt.xscale('symlog')
plt.tight_layout()
plt.savefig('latency.png')
plt.show()
114 changes: 0 additions & 114 deletions episodes/introduction.md

This file was deleted.

34 changes: 34 additions & 0 deletions episodes/optimisation-conclusion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: "Optimisation Conclusion"
teaching: 0
exercises: 0
---

:::::::::::::::::::::::::::::::::::::: questions

- What has been learnt about writing performant Python?

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


::::::::::::::::::::::::::::::::::::: objectives

- Review what has been learnt about writing performant Python

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

This concludes the optimisation portion of the course.

An overview of how Python operates and the most important practices for achieving performant code have been introduced.

Hopefully with the information from this course you will be in a better position to investigate and optimise the performance of your own code.

This course's website can be used as a reference manual when profiling your own code.

[Let us know](https://github.com/RSE-Sheffield/pando-python) what you think we've missed, so we can improve it too!

::::::::::::::::::::::::::::::::::::: keypoints

<!-- todo -->

::::::::::::::::::::::::::::::::::::::::::::::::
Loading

0 comments on commit 8a31cd6

Please sign in to comment.