I'm doing a lot of simulations with python, simulating system responses.
I've currently been using a Runge-Kutta scheme, but have stumbled upon another scheme I've been testing.
When testing this in Matlab I achieve exceptional performance, compared to that of my Runge-Kutta. However, when I transferred this to Python, it was significantly slower.
I'm not sure if this is just how it is, or if I could improve my way of coding, so I would love to hear some of your input, if possible.
The code in Matlab, exemplified:
dt = 0.0001;
f = randn(1, (60 / dt));
ns = length(f);
yo = zeros(3,1);
P1 = [0; 0.001; 0];
F = [1 0.0001 0; 0.001 1 0.0001; 0.001 0 1];
y1 = zeros(3, ns);
tic
for i = 1:ns
y1(:, i) = P1*f(:, i) + F*yo;
yo = y1(:, i);
end
toc
In which the loop executes in 0.55-0.61 sec.
The code in Python, exemplified:
dt = 0.0001
f = np.random.randn(1, int(60 / dt))
ns = np.size(f)
yo = np.zeros((3))
F = np.array([[1, 0.0001, 0], [0.001, 1, 0.0001], [0.001, 0, 1]])
P1 = np.transpose(np.array([[0, 0.0001, 0]]))
y1 = np.zeros((3, ns), order='F')
start_time = time.time()
for i in range(ns-1):
y1[:, i] = np.dot(P1, f[:, i]) + np.reshape(np.dot(F, yo), (3))
yo = y1[: , i]
print("--- %s seconds ---" % (time.time() - start_time))
In which the loop executes in 2.8 -3.1 sec.
Can I do something to improve this?
Thanks for considering my question.
scipy.integrate?numba.