我编写了一个脚本来对我们的本地集群进行一些排名顺序相关性计算。计算涉及查看两个数组,X
长度Y
为 5000-10000,并提取数量
all((X[i], Y[i]))
all((X[i], not Y[i]))
all((not X[i], Y[i]))
数千次计算(因为我洗牌X
/Y
除其他外)。
我们的一个集群正在运行 python2.4,所以我将all
s 更改为numpy.all
s。然而,我估计需要大约 5-6 小时的计算达到了 24 小时以上。这促使我进行调查。
这是一些示例代码:
In [2]: import timeit
In [3]: s = """import numpy as np
...: x, y = np.random.rand(1000), np.random.rand(1000)
...: [all((x[i], y[i])) for i in range(1000)]
...: """
In [4]: timeit.timeit(s, number=1000)
Out[4]: 0.39837288856506348
In [5]: s_numpy = """import numpy as np
...: x, y = np.random.rand(1000), np.random.rand(1000)
...: [np.all((x[i], y[i])) for i in range(1000)]
...: """
In [9]: timeit.timeit(s_numpy, number=1000)
Out[9]: 14.641073942184448
任何线索为什么numpy.all
需要 50 倍的时间来计算这个?是numpy.array
开销吗?
编辑:我的原始数组numpy.array
不像他们在这里(np.random.rand
)。all
在我需要更改线路之前,我什至根本没有使用 numpy 。但是,我已经用类似的东西替换了我的循环
np.sum(np.logical_and(X, Y))
np.sum(np.logical_and(X, np.logical_not(Y)))
np.sum(np.logical_and(np.logical_not(X), Y))
这将初始开销的运行和大约 3000 个这些循环的计算速度提高了 60% 左右。谢谢!我将寻找更多使用 numpy 进行优化的方法。