我花了很多时间来加速以下脚本,但我不能。您能帮我还是建议加快速度?这是我的代码
import numpy as np
import time
#Unchange
input = np.random.randn(300,400,500)
step_x = 30
step_y = 40
step_z = 50
lst_x = np.arange(0, input.shape[0] + step_x, step_x)
lst_y = np.arange(0, input.shape[1] + step_y, step_y)
lst_z = np.arange(0, input.shape[2] + step_z, step_z)
results = np.zeros((input.shape))
t0 = time.time()
for i in range(len(lst_x)):
for j in range(len(lst_y)):
for k in range(len(lst_z)):
xx= lst_x[i]
yy= lst_y[j]
zz= lst_z[k]
patch = input[xx : xx + step_x, yy : yy + step_y, zz : zz + step_z]
patch_added = np.random.randn(patch.shape[0],patch.shape[1],patch.shape[2]) + patch
results[xx : xx + step_x, yy : yy + step_y, zz : zz + step_z] += patch_added
print ('Time consumption: ', time.time()-t0)
您可以通过https://repl.it/repls/PleasingGhostwhiteNetworking
在线运行当前,在Repl.it上花费11秒。您能在python 3中更快吗?
更新:我有一个原始数组输入,即3D数组。从阵列中,我将提取到大小为30x40x50的补丁程序,并在该补丁程序上进行处理。之后,已处理的补丁将被分配到结果数组中,其位置与补丁在input
答案 0 :(得分:2)
更改
patch_added = np.random.randn(patch.shape[0],patch.shape[1],patch.shape[2]) + patch
到
patch_added = np.zeros(patch.shape) + patch
将“时间消耗”降低到大约2秒,而不是10-11。
这意味着脚本主要花费时间来生成随机数。