Skip to content
Merged
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
13 changes: 10 additions & 3 deletions episodes/files/pred-prey/predprey.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import math
import numpy as np
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -415,8 +416,14 @@ def run(self, random_seed=12):
# plot graph of results
self._plot()

# Argument parsing
if len(sys.argv) != 2:
print("Script expects 1 positive integer argument (number of steps), %u found."%(len(sys.argv) - 1))
sys.exit(1)
steps = int(sys.argv[1])
if steps < 1:
print("Script expects 1 positive integer argument (number of steps), %s converts < 1."%(sys.argv[1]))
sys.exit(1)



model = Model()
model = Model(steps=steps)
model.run()
5 changes: 2 additions & 3 deletions episodes/profiling-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,11 @@ Download and profile <a href="files/pred-prey/predprey.py" download>the Python p
>
> The three agents; predators, prey and grass exist in a two dimensional grid. Predators eat prey, prey eat grass. The size of each population changes over time. Depending on the parameters of the model, the populations may oscillate, grow or collapse due to the availability of their food source.

The program can be executed via `python predprey.py`.
The program can be executed via `python predprey.py <steps>`.
The value of `steps` for a full run is 250, however a full run may not be necessary to find the bottlenecks.

It takes no arguments, but contains various environment properties which can be modified to change the model's behaviour.
When the model finishes it outputs a graph of the three populations `predprey_out.png`.


:::::::::::::::::::::::: solution

It should be clear from the profile that the method `Grass::eaten()` (from `predprey.py:278`) occupies the majority of the runtime.
Expand Down
10 changes: 3 additions & 7 deletions episodes/profiling-lines.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,10 @@ from line_profiler import profile
def eaten(self, prey_list):
```

`line_profiler` can then be executed via `python -m kernprof -lvr predprey.py`.
`line_profiler` can then be executed via `python -m kernprof -lvr predprey.py <steps>`.

This will take much longer to run due to `line_profiler`, you may wish to reduce the number of steps. In this instance it may change the profiling output slightly, as the number of `Prey` and their member variables evaluated by this method both change as the model progresses, but the overall pattern is likely to remain similar.

```python
# line ~420
model = Model(50) # 50 steps (originally defaulted to 250)
```
Since this will take much longer to run due to `line_profiler`, you may wish to profile fewer `steps` than you did in the function-level profiling exercise (250 was suggested for a full run).
In this instance it may change the profiling output slightly, as the number of `Prey` and their member variables evaluated by this method both change as the model progresses, but the overall pattern is likely to remain similar.

Alternatively, you can kill the profiling process (e.g. `ctrl + c`) after a minute and the currently collected partial profiling information will be output.

Expand Down