Mar*_*cel 8 python performance numpy
我正在做类似下面的代码,我对np.roll()函数的性能不满意.我总结了baseArray和otherArray,其中baseArray在每次迭代中由一个元素滚动.但是当我滚动它时我不需要baseArray的副本,我宁愿选择一个视图,例如当我将baseArray与其他数组相加并且如果baseArray被滚动两次时,则basearray的第二个元素与第0个元素相加otherArray,baseArray的第3个元素与otherArray等的第1个元素相加.
IE实现与np.roll()相同的结果但不复制数组.
import numpy as np
from numpy import random
import cProfile
def profile():
baseArray = np.zeros(1000000)
for i in range(1000):
baseArray= np.roll(baseArray,1)
otherArray= np.random.rand(1000000)
baseArray=baseArray+otherArray
cProfile.run('profile()')
Run Code Online (Sandbox Code Playgroud)
输出(注意第3行 - 滚动功能):
9005 function calls in 26.741 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 5.123 5.123 26.740 26.740 <ipython-input-101-9006a6c0d2e3>:5(profile)
1 0.001 0.001 26.741 26.741 <string>:1(<module>)
1000 0.237 0.000 8.966 0.009 numeric.py:1327(roll)
1000 0.004 0.000 0.005 0.000 numeric.py:476(asanyarray)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1000 12.650 0.013 12.650 0.013 {method 'rand' of 'mtrand.RandomState' objects}
1000 0.005 0.000 0.005 0.000 {method 'reshape' of 'numpy.ndarray' objects}
1000 6.390 0.006 6.390 0.006 {method 'take' of 'numpy.ndarray' objects}
2000 1.345 0.001 1.345 0.001 {numpy.core.multiarray.arange}
1000 0.001 0.000 0.001 0.000 {numpy.core.multiarray.array}
1000 0.985 0.001 0.985 0.001 {numpy.core.multiarray.concatenate}
1 0.000 0.000 0.000 0.000 {numpy.core.multiarray.zeros}
1 0.000 0.000 0.000 0.000 {range}
Run Code Online (Sandbox Code Playgroud)
我很确定由于 numpy 数组内部表示的方式,不可能避免复制。数组由连续的内存地址块和一些元数据组成,其中包括数组维度、项目大小以及每个维度的元素之间的间隔(“步幅”)。向前或向后“滚动”每个元素将需要沿同一维度具有不同的长度步幅,这是不可能的。
也就是说,您可以避免在baseArray使用切片索引时复制除一个元素之外的所有元素:
import numpy as np
def profile1(seed=0):
gen = np.random.RandomState(seed)
baseArray = np.zeros(1000000)
for i in range(1000):
baseArray= np.roll(baseArray,1)
otherArray= gen.rand(1000000)
baseArray=baseArray+otherArray
return baseArray
def profile2(seed=0):
gen = np.random.RandomState(seed)
baseArray = np.zeros(1000000)
for i in range(1000):
otherArray = gen.rand(1000000)
tmp1 = baseArray[:-1] # view of the first n-1 elements
tmp2 = baseArray[-1] # copy of the last element
baseArray[1:]=tmp1+otherArray[1:] # write the last n-1 elements
baseArray[0]=tmp2+otherArray[0] # write the first element
return baseArray
Run Code Online (Sandbox Code Playgroud)
这些将给出相同的结果:
In [1]: x1 = profile1()
In [2]: x2 = profile2()
In [3]: np.allclose(x1, x2)
Out[3]: True
Run Code Online (Sandbox Code Playgroud)
实际上,性能上没有太大差异:
In [4]: %timeit profile1()
1 loop, best of 3: 23.4 s per loop
In [5]: %timeit profile2()
1 loop, best of 3: 17.3 s per loop
Run Code Online (Sandbox Code Playgroud)