为了测试 Pypy JIT 明显更快的说法,我编写了一个简单的代码,该代码重复添加两个大小为 1200x1200 的数组。我的代码如下
import numpy as np
import random
a=np.zeros((1200, 1200), dtype=np.float32)
b=np.zeros((1200, 1200), dtype=np.float32)
import timeit
#Start timer
start=timeit.default_timer()
#Initialize the arrays
for j in range(1200):
for k in range(1200):
a[j][k]=random.random()
b[j][k]=random.random()
#Repeatedly add the arrays
for j in range(10):
a=np.add(a,b)
#Stop timer and display the results
stop=timeit.default_timer()
print stop-start
Run Code Online (Sandbox Code Playgroud)
对于普通的 python,执行时间约为 1.2 - 1.5 秒。但是使用 Pypy 它会超过 15 秒吗?同样在上述情况下,我只添加了 10 次数组。如果我将此值增加到 1000,我的计算机将停止响应。我发现这是因为使用 pypy 时几乎消耗了整个 RAM。难道我做错了什么?或者问题是其他的?
假设我有两个相同尺寸的大型2-d numpy数组(比如2000x2000).我想要明智地总结它们.我想知道是否有比np.add()更快的方法
编辑:我正在添加一个类似于我现在使用的示例.有没有办法加快这个?
#a and b are the two matrices I already have.Dimension is 2000x2000
#shift is also a list that is previously known
for j in range(100000):
b=np.roll(b, shift[j] , axis=0)
a=np.add(a,b)
Run Code Online (Sandbox Code Playgroud) 我有一个图像,即一个像素值数组,比如说 5000x5000(这是典型的大小)。现在我想将它扩大 2 倍到 10kx10k。(0,0) 像素值的值在扩展图像中变为 (0,0)、(0,1)、(1,0)、(1,1)。
之后,我使用 scipy.interpolate.rotate 旋转扩展的图像(我相信没有比这更快的方法了,因为我的数组的大小)
接下来我必须再次将这个 10kx10k 数组调整为原始大小,即 5kx5k。为此,我必须取扩展图像中 (0,0)、(0,1)、(1,0)、(1,1) 的平均像素值,并将它们放入新图片。
然而事实证明,这整个过程是一个昂贵的过程,考虑到我的数组的大小,它需要很多时间。有没有更快的方法来做到这一点? 我正在使用以下代码扩展原始图像
#Assume the original image is already given
largeImg=np.zeros((10000,10000), dtype=np.float32)
for j in range(5000):
for k in range(5000):
pixel_value=original_img[j][k]
for x in range((2*k), (2*(k+1))):
for y in range((2*j), (2*(j+1))):
largeImg[y][x] = pixel_value
Run Code Online (Sandbox Code Playgroud)
使用类似的方法将旋转后的图像缩小到原始大小。
numpy ×3
python ×3
arrays ×2
image ×1
optimization ×1
performance ×1
pypy ×1
python-2.7 ×1
scipy ×1