Speedup Simpy events handling

262 views
Skip to first unread message

Daniele Medda

unread,
Apr 13, 2023, 5:17:21 AM4/13/23
to python-simpy
Hello all,
I'm running some pretty demanding  simulations (roughly 5000 LOC), with an average of 80 million events per run. You can easily imagine that, for a 2 minutes simulation time, i need to run each simulation for more than 1 hour. I tried just feeding everything to Pypy, but it actually made things worse (twice slower).
By doing some simple profiling with cProfile, most of the time is spent by handling the events on Simpy's event table. I understand that standard python lists do not perform that well with 80 million entries. Is there any way to speed things up? I'd like to avoid extreme steps like going straight back to C++ or cythonize the entire Simpy module.

Thanks for your time!

rt.van....@gmail.com

unread,
Apr 13, 2023, 8:05:16 AM4/13/23
to python-simpy
For the event mechanism, SimPy doesn't use a list, but a heapq, which is supposed to be rather efficient. And therefore difficult to speed up. And I have strong doubts about whether Cython will speed up the model significantly.
Instead, I recommend considering changing the algorithm/structuring of the problem, e.g. by only scheduling events in the near future. You could also consider estimating parts of your model with the results of a detailed model.
Without knowing anything about your model and requirements it is hard to give more concrete advice, though.

Renat Yuldashev

unread,
Oct 18, 2023, 5:06:04 AM10/18/23
to python-simpy
In my experience Scalene is better than CProfile. It is more accurate and highlights memory problems. You may also try latest python 3.11 or even 3.12.
p.s. there are no silver bullets

Reply all
Reply to author
Forward
0 new messages