Skip to content

Commit

Permalink
Merge pull request #4 from DHI/profiling
Browse files Browse the repository at this point in the history
Profiling code
  • Loading branch information
ecomodeller authored Oct 17, 2023
2 parents cb8be73 + 65b6421 commit eefe686
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions 04_testing.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,78 @@ Visual Studio Code can be configured to run `black` automatically when saving a



## Profiling

::: {.incremental}

* Profiling is a way to measure the performance of your code.
* It can help you identify bottlenecks in your code.
* Your intuition about what is slow is often wrong.
* The `line_profiler` package reports the time spent on each line of code.
* It can be run inside a notebook using the `lprun` magic command.

:::

## Profiling - *example code* {.smaller}

```python
import numpy as np

def top_neighbors(points, radius="0.1"):
"""Don't use this function, it's only purpose is to be profiled."""
n = len(points)
idx = np.array([int(x) for x in str.split("0 "* n)])

for i in range(n):
for j in range(n):
if i != j:
d = np.sqrt(np.sum((points[i] - points[j])**2))
if d < float(radius):
idx[i] += 1
for i in range(n):
for j in range(n - i - 1):
if idx[j] < idx[j + 1]:
idx[j], idx[j + 1] = idx[j + 1], idx[j]
points[j], points[j + 1] = points[j + 1], points[j]
return points

def main():
points = np.random.rand(1000, 2)
top = top_neighbors(points)
```

## Profiling - *output* {.smaller}

Invoking the jupyter magic command `lprun` with:

* function to profile - `top_neighbors`
* code to run - `main()`

```python
%lprun -f top_neighbors main()
```
. . .

```
Line # Hits Time Per Hit % Time Line Contents
==============================================================
3 def top_neighbors(points, radius="0.1"):
4 """Don't use this function, it's only purpose is to be profiled."""
5 1 2800.0 2800.0 0.0 n = len(points)
6 1 353300.0 353300.0 0.0 idx = np.array([int(x) for x in str.split("0 "* n)])
7
8 1001 345100.0 344.8 0.0 for i in range(n):
9 1001000 378191701.0 377.8 2.2 for j in range(n):
10 1000000 328387205.0 328.4 1.9 if i != j:
11 999000 1e+10 14473.0 83.8 d = np.sqrt(np.sum((points[i] - points[j])**2))
12 999000 933778605.0 934.7 5.4 if d < float(radius):
13 28952 57010001.0 1969.1 0.3 idx[i] += 1
14 1001 367100.0 366.7 0.0 for i in range(n):
15 500500 144295203.0 288.3 0.8 for j in range(n - i - 1):
16 499500 302166901.0 604.9 1.8 if idx[j] < idx[j + 1]:
17 240227 212070500.0 882.8 1.2 idx[j], idx[j + 1] = idx[j + 1], idx[j]
18 240227 437538803.0 1821.4 2.5 points[j], points[j + 1] = points[j + 1], points[j]
19 1 500.0 500.0 0.0 return points
```

Binary file removed academy_logo.png
Binary file not shown.

0 comments on commit eefe686

Please sign in to comment.