1

我有以下代码:

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
4

0 回答 0