Skip to content

The benchmark in the ReadMe seems to execute a too slow pure-python version #25

@Tortar

Description

@Tortar

I stumbled across this repository, and while the approach seems interesting, actually the benchmark compares apple-to-oranges because the pure Python version is way too slow than what is expected. The running time seems also quadratic, which doesn't make a lot of sense to me because the money model seems linear in the number of agents. By digging into the source code I think that the culprit is at https://github.com/adamamer20/mesa-frames/blob/main/docs/scripts/readme_plot.py#L31 because self.model.schedule.agents creates a copy of the structure containing the agents, which makes the model much slower than what it should be, and explains also the quadratic behaviour.

Indeed an Agents.jl version of the model is still like 40x faster than Polars:

julia> using Agents, Random

julia> @agent struct WealthAgent(NoSpaceAgent)
           wealth::Int
       end

julia> function wealth_model(; numagents = 100, initwealth = 1)
           model = ABM(WealthAgent; agent_step!, scheduler = Schedulers.Randomly(), 
                       rng = Xoshiro(42), container = Vector)
           for _ in 1:numagents
               add_agent!(model, initwealth)
           end
           return model
       end
wealth_model (generic function with 1 method)

julia> function agent_step!(agent, model)
           agent.wealth == 0 && return
           agent.wealth -= 1
           random_agent(model).wealth += 1
       end
agent_step! (generic function with 1 method)

julia> m = wealth_model(; numagents=9000);

julia> @time step!(m, 1);
  0.067163 seconds (94.95 k allocations: 4.839 MiB, 99.34% compilation time)

julia> @time step!(m, 100);
  0.011685 seconds

Hope this helps you to find a better benchmark :-)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions