I want to create a program for multi-agent simulation and I am thinking about whether I should use NumPy or numba to accelerate the calculation. Basically, I would need a class to store the state of agents and I would have over a 1000 instances of this classes. In each time step, I will perform different calculation for all instances. There are two approaches that I am thinking of:
Numpy vectorization:
Having 1 class with multiple NumPy arrays for storing states of all agents. Hence, I will only have 1 class instance at all times during the simulation. With this approach, I can simply use NumPy vectorization to perform calculations. However, this will make running functions for specific agents difficult and I would need an extra class to store the index of each agent.
Class agent:
def __init__(self):
self.A = np.zeros(1000)
self.B = np.zeros(1000)
def timestep(self):
return self.A + self.B
Numba jitclass:
Using the numba jitclass decorator to compile the code. With this approach, I can apply more standard OOP formatted code as one class instance represent one agent. However, I am not sure about the performance of looping through multiple jitclass instance (say 1000 and more).
@jitclass
class agent:
def __init__(self):
self.A = 0
self.B = 0
def timestep(self):
return self.A + self.B
I would like to know which would be a faster approach.