1 Jan 20:07
fast iteration (I think I've got it)
Neal Becker <ndbecker2 <at> gmail.com>
2008-01-01 19:07:33 GMT
2008-01-01 19:07:33 GMT
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. I put together a small test, which seems to work for the cases I tried. The idea is to use the general iterator (via PyArray_IterAllButAxis), but iteration over the last dimension is done with a c-style pointer. I have tested it with these cases, and the results appear correct: a = array ((xrange(4),xrange(4,8),xrange(8,12)), int) b = a[:,::2] c = a[:,1::2] d = a[:,-1::-1] sum3(a) 0 1 2 3 4 5 6 7 8 9 10 11 sum3(b) 0 2 4 6(Continue reading)
RSS Feed