Skip to content

Commit

Permalink
Episode: Function Level Profiling (#6)
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
Robadob committed Jan 10, 2024
1 parent 4cfde7f commit 611215c
Show file tree
Hide file tree
Showing 10 changed files with 800 additions and 6 deletions.
Binary file added episodes/fig/snakeviz-home.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/snakeviz-worked-example-icicle.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/snakeviz-worked-example-sunburst.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
421 changes: 421 additions & 0 deletions episodes/files/pred-prey/predprey.py

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions episodes/files/snakeviz-worked-example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import time

"""
This is a synthetic program intended to produce a clear profile with cProfile/snakeviz
Method names, constructed from a hex digit and a number clearly denote their position in the hierarchy.
"""
def a_1():
for i in range(3):
b_1()
time.sleep(1)
b_2()


def b_1():
c_1()
c_2()

def b_2():
time.sleep(1)

def c_1():
time.sleep(0.5)


def c_2():
time.sleep(0.3)
d_1()

def d_1():
time.sleep(0.1)




# Entry Point
a_1()
Binary file added episodes/files/snakeviz-worked-example/out.prof
Binary file not shown.
48 changes: 48 additions & 0 deletions episodes/files/travelling-sales/travellingsales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Naive brute force travelling salesperson
python travellingsales.py <cities>
"""

import itertools
import math
import random
import sys

def distance(point1, point2):
return math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)

def total_distance(points, order):
total = 0
for i in range(len(order) - 1):
total += distance(points[order[i]], points[order[i + 1]])
return total + distance(points[order[-1]], points[order[0]])

def traveling_salesman_brute_force(points):
min_distance = float('inf')
min_path = None
for order in itertools.permutations(range(len(points))):
d = total_distance(points, order)
if d < min_distance:
min_distance = d
min_path = order
return min_path, min_distance

# Argument parsing
if len(sys.argv) != 2:
print("Script expects 1 positive integer argument, %u found."%(len(sys.argv) - 1))
sys.exit()
cities_len = int(sys.argv[1])
if cities_len < 1:
print("Script expects 1 positive integer argument, %s converts < 1."%(sys.argv[1]))
sys.exit()
# Define the cities as (x, y) coordinates
random.seed(12) # Fixed random for consistency
cities = [(0,0)]
for i in range(cities_len):
cities.append((random.uniform(-1, 1), random.uniform(-1, 1)))

# Find the shortest path
shortest_path, min_distance = traveling_salesman_brute_force(cities)
print("Cities:", cities_len)
print("Shortest Path:", shortest_path)
print("Shortest Distance:", min_distance)
295 changes: 292 additions & 3 deletions episodes/profiling-functions.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions episodes/profiling-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Function-level profiling analyses where time is being spent with respect to func
This allows functions that occupy a disproportionate amount of the total runtime to be quickly identified and investigated.

<!-- We will be covering -->
In this course we will cover the usage of the function-level profiler `cprofile` and how it's output can be visualised with `snakeviz`.
In this course we will cover the usage of the function-level profiler `cProfile` and how it's output can be visualised with `snakeviz`.

### Line-Level Profiling
<!-- Context -->
Expand Down Expand Up @@ -205,7 +205,7 @@ Write a short plan of the approach you would take to investigate and confirm whe
- Profiling is a relatively quick process to analyse where time is being spent and bottlenecks during a program's execution.
- Code should be profiled when ready for deployment if it will be running for more than a few minutes during it's lifetime.
- There are several types of profiler each with slightly different purposes.
- function-level: `cprofile` (visualised with `snakeviz`)
- function-level: `cProfile` (visualised with `snakeviz`)
- line-level: `line_profiler`
- timeline: `viztracer`
- A representative test-case should be profiled, that is large enough to amplify any bottlenecks whilst executing to completion quickly.
Expand Down
2 changes: 1 addition & 1 deletion learners/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This course uses Python and was developed using Python 3.11, therefore it is rec

The non-core Python packages required by the course are `snakeviz`, `line_profiler` and `viztracer` which can be installed via `pip`.

```input
```sh
pip install snakeviz line_profiler[all] viztracer[full]
```

Expand Down

0 comments on commit 611215c

Please sign in to comment.