我有以下代码:
def rotation_cpu(img, theta, dst):
cosTheta = np.cos(theta)
sinTheta = np.sin(theta)
for i in range(512):
for j in range(512):
xpos = cosTheta * i - sinTheta * j
ypos = sinTheta * i + cosTheta * j
dst[xpos + 725/2, ypos ] = img[i,j]
我正在测试它,我得到1.7300620079
秒。但是,当我使用@autojit
装饰器时,它变得更糟。
from numbapro import autojit
@autojit
def rotation_cpu(img, theta, dst):
cosTheta = np.cos(theta)
sinTheta = np.sin(theta)
for i in range(512):
for j in range(512):
xpos = cosTheta * i - sinTheta * j
ypos = sinTheta * i + cosTheta * j
dst[xpos + 725/2, ypos ] = img[i,j]
1.92721390724
秒。我应该换一些东西来获得更好的表现吗?
测试代码是:
from timeit import default_timer as timer
from scipy.misc import imread
img = imread("pic.jpg") # it is a 512x512 pic.
theta = 45
ts = timer()
dst = np.zeros((725, 725)) # new boundaries
rotation_cpu(img, theta * np.pi / 180, dst)
te = timer()
print te - ts