# Predator\_prey\_multiagent vs predator\_prey\_fast.jl example

**URL:** <https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359>\
**Category:** Modelling & Simulations\
**Tags:** agents\
**Created:** [October 7, 2021, 10:23am UTC](https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359 "2021-10-07T10:23:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edu\_P](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edu_p/32/29741_2.png) [@Edu\_P](https://discourse.julialang.org/u/Edu_P)\
**Post date:** [October 7, 2021, 10:23am UTC](https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359/1 "2021-10-07T10:23:33Z")

</div>

Hello, in the predator\_prey\_multiagent.jl example I read the following comment: “This approach is **less performant** than the version in [Predator-prey dynamics]”.  
Has someone more details on what makes it a less performant implementation than the predator\_prey\_fast.jl version?

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [October 7, 2021, 10:45am UTC](https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359/2 "2021-10-07T10:45:21Z")

</div>

Trying to put it in two sentences (and please correct me if I’m wrong here @Libbum or @Datseris):  
The performance problem lies in dynamic dispatch during runtime because the compiler cannot optimise dispatch for the concrete agent types beforehand. The faster version deals with this by simply introducing if/else clauses for the different agent types inside the `model_step!` function instead of relying on dispatching to the correct version of `agent_step!` for each agent type.

---

<div class="post-metadata">

**Author:** ![Libbum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/libbum/32/3935_2.png) [@Libbum](https://discourse.julialang.org/u/Libbum)\
**Post date:** [October 7, 2021, 11:12am UTC](https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359/3 "2021-10-07T11:12:47Z")

</div>

Yes, this is exactly the issue.

For more information, you can take a look at the discussion in [this open issue](https://github.com/JuliaDynamics/Agents.jl/issues/445).

We’d like to try [Catwalk.jl](https://github.com/tisztamo/Catwalk.jl) as a method to auto-optimise this problem, or there are design discussions in the works that will try to give us a cleaner interface for multi-agent model types without dynamic dispatch performance losses.

Luckily, for a few agent types, the performance drop is quite small. But when you’re looking in the 10s or 100s of types, things are noticeable.

---

<div class="post-metadata">

**Author:** ![Edu\_P](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edu_p/32/29741_2.png) [@Edu\_P](https://discourse.julialang.org/u/Edu_P)\
**Post date:** [October 7, 2021, 11:22am UTC](https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359/4 "2021-10-07T11:22:28Z")

</div>

That helps a lot. Thanks!

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [October 7, 2021, 1:34pm UTC](https://discourse.julialang.org/t/predator-prey-multiagent-vs-predator-prey-fast-jl-example/69359/5 "2021-10-07T13:34:00Z")

</div>

> [@Libbum](#):
>
> Luckily, for a few agent types, the performance drop is quite small. But when you’re looking in the 10s or 100s of types, things are noticeable.

It could also be that the performance cost of dynamic dispatch is negligible for simulations that either run for a longer time or contain computationally expensive calculations/functions. However, this is still mostly unexplored and we will have to devote some time on this topic in the future. See [issue #547](https://github.com/JuliaDynamics/Agents.jl/issues/547).
