I have a script that requires multiple loops. The Python version takes ~88 seconds while the MATLAB version takes ~9 seconds. I am already using vectorization to remove one of the loops.
MATLAB:
ntotal = 2000;
nj = 4000;
r = zeros(ntotal,nj,3);
ncorr = 200;
temp2 = zeros(ncorr,nj);
final = zeros(3,ncorr);
tic
for k = 1:3
for j = 1:nj
for n0 = 1:ncorr
temp1=(r(1:ncorr-n0,j,k)-r(n0:ncorr-1,j,k)).^2;
temp2(n0,j) = mean(temp1);
end
end
final(k,:) = mean(temp2,2);
end
toc
Python:
import time
import numpy as np
ntotal = 2000
nj = 4000
r = np.zeros((ntotal,nj,3))
ncorr = 200
temp2 = np.zeros((ncorr,nj))
final = np.zeros((3,ncorr))
t0 = time.time()
for k in range(3):
for j in range(nj):
for n0 in range(ncorr):
temp1 = (r[0:ntotal-n0,j,k]-r[n0:ntotal,j,k]) ** 2
temp2[n0,j] = np.mean(temp1)
final[k,:] = np.mean(temp2,axis=1)
t1 = time.time()
print(t1-t0)
When I increased nj to 10000, the timings became 21 (MATLAB) vs 248 seconds (Python). Any advice on how to make the Python script run faster? Thanks.
Try to use cython, it's a staticly typed, compiled superset of python, it really makes the code faster, particullary with nested loops. I think you could have a better result than with MATLAB.