Model Musings
SimPy Performance Optimisation: Making Simulations Faster
- Published
- Reading time
- 10 min
Your simulation works. But it’s slow. Here’s how to fix that.
Profile First
Don’t guess. Measure. Here is a complete model of a two-server counter with three habits that slow models tend to share: a monitor that wakes ten times a minute, a debug message formatted for every customer and a brand new random number generator for every draw. Save it as model.py and run it:
import cProfile
import logging
import pstats
import time
import numpy as np
import simpy
logging.basicConfig(level=logging.INFO) # Debug messages are switched off
logger = logging.getLogger("bank")
def customer(env, counter, cid, waits):
logger.debug(f"Customer {cid} arrives at {env.now:.2f}, queue {len(counter.queue)}")
arrived = env.now
with counter.request() as req:
yield req
waits.append(env.now - arrived)
yield env.timeout(np.random.default_rng().exponential(5))
def arrivals(env, counter, waits):
cid = 0
while True:
yield env.timeout(np.random.default_rng().exponential(3))
env.process(customer(env, counter, cid, waits))
cid += 1
def monitor(env, counter, queue_lengths):
while True:
queue_lengths.append(len(counter.queue))
yield env.timeout(0.1)
def run_simulation(run_time=100_000):
env = simpy.Environment()
counter = simpy.Resource(env, capacity=2)
waits, queue_lengths = [], []
env.process(arrivals(env, counter, waits))
env.process(monitor(env, counter, queue_lengths))
env.run(until=run_time)
return waits
start = time.perf_counter()
run_simulation()
print(f"Unprofiled run: {time.perf_counter() - start:.2f} seconds")
# Profile
profiler = cProfile.Profile()
profiler.enable()
run_simulation()
profiler.disable()
# Analyse
stats = pstats.Stats(profiler).strip_dirs()
stats.sort_stats('cumulative')
stats.print_stats(12) # The twelve biggest consumers of time
On my machine (Python 3.14, SimPy 4.1.2) it printed this; your timings will differ, the ranking should not:
Unprofiled run: 1.68 seconds
15984011 function calls in 4.066 seconds
Ordered by: cumulative time
List reduced from 64 to 12 due to restriction <12>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 4.066 4.066 model.py:32(run_simulation)
1 0.188 0.188 4.066 4.066 core.py:214(run)
1199223 0.389 0.000 3.878 0.000 core.py:182(step)
1132816 0.364 0.000 3.241 0.000 events.py:388(_resume)
1132816 0.177 0.000 2.764 0.000 {method 'send' of 'generator' objects}
1000000 0.428 0.000 1.162 0.000 model.py:27(monitor)
99611 0.317 0.000 0.873 0.000 model.py:12(customer)
1066409 0.232 0.000 0.657 0.000 events.py:230(__init__)
33205 0.247 0.000 0.551 0.000 model.py:20(arrivals)
1199227 0.284 0.000 0.489 0.000 core.py:165(schedule)
66409 0.230 0.000 0.263 0.000 _ufunc_config.py:496(inner)
2298836 0.183 0.000 0.183 0.000 {method 'append' of 'list' objects}
Read it from the top. Below run_simulation itself come SimPy’s own functions (run, step, _resume), busy because they process 1.2 million events. Where do those come from? The first of our functions in the list answers that: monitor ran 1,000,000 times (cProfile counts every resumption of a generator as a call), once per tenth of a minute for 100,000 minutes; on its own it accounts for more than a quarter of the run. Then arrivals repays a second look: 33,205 calls cost 0.551 seconds, about 17 microseconds each, for a function that only draws a number and starts a process. The cost hides inside a NumPy call that cProfile cannot see into: np.random.default_rng() builds a fresh generator, seeded from the operating system, for every single draw. The switched-off debug messages do not make the top twelve at all, which is precisely why you profile before you optimise. Note, too, that profiling more than doubled the run time (1.68 seconds without it, 4.07 with), so read the report for proportions rather than absolute times.
Find the bottleneck before optimising.
Common Bottlenecks
1. Too Many Events
Every timeout creates an event. Reduce them:
# SLOW - event every 0.1 time units
def monitor(env, resource):
while True:
record_stats(resource)
yield env.timeout(0.1) # 10,000 events per 1,000 time
# FASTER - event every 10 time units
def monitor(env, resource):
while True:
record_stats(resource)
yield env.timeout(10) # 100 events per 1,000 time
2. Expensive Logging
# SLOW - string formatting on every event
def customer(env, server, cid):
logger.debug(f"Customer {cid} arrived at {env.now} with queue {len(server.queue)}")
# ... rest of process
# FASTER - check level first
def customer(env, server, cid):
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Customer {cid} arrived at {env.now}")
# ... rest of process
# FASTEST - no logging in production
DEBUG = False
def customer(env, server, cid):
if DEBUG:
print(f"Customer {cid} arrived")
3. Slow Random Number Generation
import numpy
# SLOW - creating generator each time
def get_service_time():
return numpy.random.default_rng().exponential(5)
# FAST - reuse generator
rng = numpy.random.default_rng(42)
def get_service_time():
return rng.exponential(5)
Reuse matters more than which generator you reuse, but the choice is not free either. On my machine a fresh NumPy generator per draw took about 8 microseconds, a reused one 0.3 and Python’s own random.expovariate 0.08. NumPy’s overhead is per call, so it pays for itself when you ask for a whole array of numbers at once, not one number at a time.
4. Memory That Grows Without Limit
# Memory grows with every customer
wait_times = []
def customer(env, server):
# ...
wait_times.append(wait) # Fast, but the list never stops growing
# Bounded memory: keep a rolling window
from collections import deque
wait_times = deque(maxlen=10000) # Keep last 10,000
# Constant memory: compute statistics incrementally
class IncrementalStats:
def __init__(self):
self.count = 0
self.mean = 0.0
self.m2 = 0.0 # Sum of squared deviations from the mean
def add(self, value):
# Welford's update: stays accurate even when the mean dwarfs the spread
self.count += 1
delta = value - self.mean
self.mean += delta / self.count
self.m2 += delta * (value - self.mean)
@property
def variance(self):
if self.count < 2:
return 0
return self.m2 / (self.count - 1)
None of these is a speed-up. list.append is about as cheap as Python gets, and IncrementalStats.add costs more per call than an append, because it is a Python method doing arithmetic. What they save is memory: a list of every wait in a long run can outgrow your RAM, and a model that is swapping to disk is slow for reasons no profiler will pin on a single line.
Reduce Object Creation
# A dict per customer: quick to create, but the heaviest to keep
def customer(env, server, cid):
data = {
'arrival': env.now,
'cid': cid,
'wait': 0,
'service': 0
}
# ...
# A dataclass is clearer, though the slowest of the three to create
from dataclasses import dataclass
@dataclass
class CustomerData:
arrival: float
cid: int
wait: float = 0
service: float = 0
# Slots drop the per-instance dict: as quick to create as a dict, and far smaller
@dataclass(slots=True)
class CustomerData:
arrival: float
cid: int
wait: float = 0
service: float = 0
Measure this one rather than assume it. On Python 3.14 the dict literal and the slotted dataclass are about equally quick to create, at a median of roughly 50 to 55 nanoseconds each over 63 timed rounds; the plain dataclass is the slowest at about 80. Speed, then, is no reason to choose slots. What slots really buy is memory: 64 bytes per record against 184 for the dict, which matters once you keep millions of them. Better still, keep fewer, as the memory section below shows.
Batch Processing
# SLOW - process one at a time
def arrivals(env, server):
while True:
yield env.timeout(random.expovariate(1))
env.process(customer(env, server))
# FASTER - batch arrivals when appropriate
def batch_arrivals(env, server, batch_size=10):
while True:
# Wait for batch interval
yield env.timeout(10)
# Create batch
for _ in range(batch_size):
env.process(customer(env, server))
Only batch when the real system batches. Ten customers arriving together every ten minutes is a different queue from one customer a minute, and a faster wrong answer is no bargain.
Use PyPy
PyPy, a Python with a just-in-time compiler, often runs pure-Python code such as SimPy’s event loop several times faster. Libraries written in C, NumPy among them, gain little and can even run slower, so time your own model before you commit:
# Install PyPy
# Then install SimPy in PyPy environment
pypy -m pip install simpy
# Run simulation
pypy simulation.py
Parallel Replications
Replications are independent, so they spread across processor cores with no change to the model. A complete example:
import os
import random
import time
from multiprocessing import Pool
import simpy
def run_single_replication(seed, run_time=20_000):
"""Run one replication; return the mean wait."""
rng = random.Random(seed)
env = simpy.Environment()
counter = simpy.Resource(env, capacity=2)
waits = []
def customer():
arrived = env.now
with counter.request() as req:
yield req
waits.append(env.now - arrived)
yield env.timeout(rng.expovariate(1 / 5))
def arrivals():
while True:
yield env.timeout(rng.expovariate(1 / 3))
env.process(customer())
env.process(arrivals())
env.run(until=run_time)
return sum(waits) / len(waits)
# Keep the guard: Python re-imports this file to start the workers on
# Windows and macOS, and on Linux too from Python 3.14
if __name__ == "__main__":
seeds = range(100)
start = time.perf_counter()
sequential = [run_single_replication(s) for s in seeds]
print(f"Sequential: {time.perf_counter() - start:.2f} seconds")
start = time.perf_counter()
with Pool() as pool:
parallel = pool.map(run_single_replication, seeds)
print(f"Parallel: {time.perf_counter() - start:.2f} seconds on {os.cpu_count()} cores")
print("Same results:", parallel == sequential)
On my machine:
Sequential: 3.87 seconds
Parallel: 0.94 seconds on 12 cores
Same results: True
The answers are identical, because each replication seeds its own generator; the speed-up is about four, not twelve. Three things eat into it: starting the worker processes and passing results between them costs time, each replication here takes only a few hundredths of a second and my machine had other work on at the time. The longer each replication runs, the nearer you get to the full core count.
Simplify the Model
Sometimes the fastest code is code you don’t run:
# DETAILED - every step modelled
def detailed_customer(env, reception, triage, doctor, pharmacy):
yield from visit_reception(env, reception)
yield from visit_triage(env, triage)
yield from visit_doctor(env, doctor)
yield from visit_pharmacy(env, pharmacy)
# SIMPLIFIED - aggregate what you don't need
def simplified_customer(env, system):
# Model reception + triage as single step
yield env.timeout(random.triangular(2, 10, 5))
# Model doctor (what we care about)
with system.request() as req:
yield req
yield env.timeout(random.expovariate(1/15))
Pre-generate Random Numbers
# On demand: simple, and quick enough for most models
def customer(env, server):
service_time = random.expovariate(1/5)
yield env.timeout(service_time)
# Pre-generated: one NumPy call draws the lot
class Simulation:
def __init__(self, n_customers):
rng = numpy.random.default_rng(42)
self.service_times = rng.exponential(5, size=n_customers)
self.current = 0
def next_service_time(self):
time = self.service_times[self.current] # IndexError if you run out
self.current += 1
return time
Measure before you bother. On the same two-server queue, drawing every arrival and service time up front saved under ten per cent against calling random.expovariate as needed. The better reason to pre-generate is control: every customer’s numbers are fixed before the run begins, so two scenarios can be fed exactly the same customers. Size the array generously, or the run ends in an IndexError.
Avoid String Operations
# SLOW
def customer(env, server, cid):
name = f"customer_{cid}" # String created
log_arrival(name, env.now) # More string operations
# FAST - use IDs
def customer(env, server, cid):
log_arrival(cid, env.now) # Just integers
Memory Management
# HEAVY - keep everything
class Simulation:
def __init__(self):
self.all_customers = []
def customer(self, env, server, cid):
data = CustomerData(env.now, cid)
self.all_customers.append(data) # Memory grows forever
# LEAN - keep only what you need
class Simulation:
def __init__(self):
self.stats = IncrementalStats()
self.recent = deque(maxlen=1000)
def customer(self, env, server, cid):
# Update stats, don't store everything
self.stats.add(wait_time)
self.recent.append((env.now, wait_time))
Optimised Example
The model from the top of this post, with the lessons applied: no monitor, no logging, one generator reused for every draw (Python’s own random, which is quicker than NumPy one number at a time) and running statistics in place of a list:
import random
import time
import simpy
class FastSimulation:
def __init__(self, seed=42):
self.rng = random.Random(seed) # One generator for the whole run
self.env = simpy.Environment()
self.server = simpy.Resource(self.env, capacity=2)
# Running statistics instead of a list of every wait
self.wait_count = 0
self.wait_mean = 0.0
self.wait_m2 = 0.0
self.wait_max = 0.0
def record_wait(self, wait):
"""Incremental statistics (Welford): O(1) time, O(1) space."""
self.wait_count += 1
delta = wait - self.wait_mean
self.wait_mean += delta / self.wait_count
self.wait_m2 += delta * (wait - self.wait_mean)
if wait > self.wait_max:
self.wait_max = wait
def customer(self):
arrival = self.env.now
with self.server.request() as req:
yield req
self.record_wait(self.env.now - arrival)
yield self.env.timeout(self.rng.expovariate(1 / 5))
def arrivals(self):
while True:
yield self.env.timeout(self.rng.expovariate(1 / 3))
self.env.process(self.customer())
def run(self, duration):
self.env.process(self.arrivals())
self.env.run(until=duration)
def results(self):
n = self.wait_count
var = self.wait_m2 / (n - 1) if n > 1 else 0.0
return {
'customers': n,
'mean_wait': round(self.wait_mean, 2),
'std_wait': round(var ** 0.5, 2),
'max_wait': round(self.wait_max, 2)
}
# Run
start = time.perf_counter()
sim = FastSimulation()
sim.run(100_000)
print(sim.results())
print(f"{time.perf_counter() - start:.2f} seconds")
On my machine:
{'customers': 33154, 'mean_wait': 10.73, 'std_wait': 13.29, 'max_wait': 106.99}
0.18 seconds
Under a fifth of a second for the same 100,000 minutes that took 1.68 seconds at the top of the post: about nine times faster, and most of the gain comes from the two culprits the profile named, the monitor and the generator built for every draw. Measured on the same machine in the same session; yours will differ, but the shape of the result should not.
Summary
Speed up SimPy:
- Profile first - find the real bottleneck
- Reduce events - fewer timeouts
- Cut logging - check level before formatting
- Reuse objects - one random generator for the whole run
- Parallelise replications - use multiprocessing
- Simplify model - don’t model what you don’t need
Keep memory flat:
- Compute incrementally - don’t store everything
- Use slots - for the records you must keep
Measure. Optimise. Measure again.