[Numpy-discussion] fast iteration (I think I've got it)
Travis E. Oliphant
oliphant@enthought....
Tue Jan 1 15:37:54 CST 2008
Neal Becker wrote:
> This is a c-api question.
>
> I'm trying to get iterators that are both fast and reasonably general. I
> did confirm that iterating using just the general PyArrayIterObject
> protocol is not as fast as using c-style pointers for contiguous arrays.
>
> Please confirm if my understanding is correct. There are 2 cases to
> consider. There are plain old dense arrays, which will be contiguous, and
> there are array 'views' or 'slices'. The views will not be contiguous.
> However, in all cases, the strides between elements in one dimension are
> constant. This last point is key. As long as the stride in the last
> dimension is a constant, a fast iterator is possible.
>
Your understanding is correct. You can use the iterator over all but
one dimension. This is a common paradigm in NumPy (use an iterator over
all but one dimension (usually chosen to be the one with the smallest
striding) and then use strided access over the final dimension (see all
the ufuncs for examples).
IterAllButAxis takes a pointer to an integer. If that integer is
negative on input, then IterAllButAxis will choose the axis for you to
be the one with the smallest stride, then it will set the variable to
the axis that was chosen.
This is just what ufuncs do and is a pretty good idea generally to
minimize the over-head of the final inner loop.
-Travis O.
More information about the Numpy-discussion
mailing list