One way to speed up operations on numpy data is to use vectorize . Essentially, vectorize takes a function f and creates a new function g that maps f to an array a . g then called g(a) : a g(a) .
>>> sqrt_vec = numpy.vectorize(lambda x: x ** 0.5) >>> sqrt_vec(numpy.arange(10)) array([ 0. , 1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
Without the data that you work with, I cannot say for sure whether this will help, but perhaps you can rewrite the above as a set of functions that can be vectorized . Perhaps in this case you could vectorize an array of indices in ReadAsArray(h,i,numberColumns, numberRows) . Here is an example of potential benefits:
>>> print setup1 import numpy sqrt_vec = numpy.vectorize(lambda x: x ** 0.5) >>> print setup2 import numpy def sqrt_vec(a): r = numpy.zeros(len(a)) for i in xrange(len(a)): r[i] = a[i] ** 0.5 return r >>> timeit.timeit(stmt='a = sqrt_vec(numpy.arange(1000000))', setup=setup1, number=1) 0.30318188667297363 >>> timeit.timeit(stmt='a = sqrt_vec(numpy.arange(1000000))', setup=setup2, number=1) 4.5400981903076172
15x acceleration! Also note that numpy slicing handles the edges of ndarray elegantly:
>>> a = numpy.arange(25).reshape((5, 5)) >>> a[3:7, 3:7] array([[18, 19], [23, 24]])
So, if you could get your ReadAsArray data in ndarray , you would not need to do any Hennigan checks.
Regarding your question about reformation - restructuring does not fundamentally change the data at all. It just changes the "steps" by which numpy indexes the data. When you call the reshape method, the return value is a new kind of data; data is not copied or changed at all, as well as the old view with the old step information.
>>> a = numpy.arange(25) >>> b = a.reshape((5, 5)) >>> a array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]) >>> b array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) >>> a[5] 5 >>> b[1][0] 5 >>> a[5] = 4792 >>> b[1][0] 4792 >>> a.strides (8,) >>> b.strides (40, 8)