I am trying to rewrite the MATLAB code below into Python, and found that my Python code (2.7 sec) is slower than MATLAB (1.2 sec). I have tried many different ways, including the module numba, but no luck yet. How can I make the Python code faster?
MATLAB code:
szA=[1024,1280]; HfszA=[512,640];
[aPx,aPy]=meshgrid(-HfszA(2):HfszA(2)-1,-HfszA(1):HfszA(1)-1);
img=randi(255,1024,1280);
fx=rand(); fy=rand();
tic
for i=1:20
F=abs(sum(sum(img.*exp(-1i*2*pi*(fx*aPx+fy*aPy)))));
end
toc
Python code:
import numpy as np
import time
szA=[1024,1280]; HfszA=[512,640]
aPx,aPy=np.meshgrid(np.arange(-HfszA[1],HfszA[1]),np.arange(-HfszA[0],HfszA[0]))
img=np.array(np.random.randint(256,size=(1024,1280)))
fx=np.random.rand()
fy=np.random.rand()
start = time.time()
for i in range(20):
F=abs(np.sum(img*np.exp(-1j*2*np.pi*(fx*aPx+fy*aPy))))
end = time.time()
print("Elapsed (after compilation) = %s" % (end - start))
print(F)