Question 1:
Since slicing an array may sometimes require a copy of the underlying data structure (holding pointers to data in memory), they can be quite expensive. If you are actually nodes of this in the above example, you can perform average operations by actually iterating the elements i to i + 10 and manually creating the average value. For some operations this will not give any performance improvement, but avoiding the creation of new data structures will generally speed up the process.
One more note: if you do not use native types inside numpy, you will get a very big performance limitation for managing a numpy array. Suppose your array has dtype = float64, and your own float32 fleet size - it will cost a lot of extra computing power for numpy and overall performance. This is sometimes normal, and you can just take a hit to maintain the data type. In other cases, it is arbitrary what type of float or int is stored as internally. In these cases, try dtype = float instead of dtype = float64. Numpy should use its own type by default. Having made this change, I had 3x + acceleration by intensive use algorithms.
Question 2:
__array_finalize__ "is called whenever the system internally allocates a new array from obj, where obj is a subclass (subtype) of (large) ndarray" according to SciPy . So this is the result described in the first question. When you slice and create a new array, you must complete this array by making structural copies or wrapping the original structure. This operation takes time. Avoiding fragments will save on this operation, although for multidimensional data it is impossible to completely avoid calls to __array_finalize__ .
Pyrce source share