diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2013-08-20 19:56:48 +0200 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2013-08-30 19:49:44 +0200 |
commit | c5ee7f75d685ab520da528898df2e5570d0ee7f6 (patch) | |
tree | 0e67d98fa8bbd9713106a707e70d5c193add0305 | |
parent | 501889408258ef1a5ffa842d8c0ecc7ffa6f5712 (diff) | |
download | numpy-c5ee7f75d685ab520da528898df2e5570d0ee7f6.tar.gz |
ENH: add nonzero of bools fastpath
count_nonzero already has a fastpath but nonzero does not.
Improves performance by about 25% via avoiding a function call.
-rw-r--r-- | numpy/core/src/multiarray/item_selection.c | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index 81338ee32..bcaf9fed7 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -2586,11 +2586,22 @@ PyArray_Nonzero(PyArrayObject *self) stride = (ndim == 0) ? 0 : PyArray_STRIDE(self, 0); count = (ndim == 0) ? 1 : PyArray_DIM(self, 0); - for (j = 0; j < count; ++j) { - if (nonzero(data, self)) { - *multi_index++ = j; + if (PyArray_ISBOOL(self)) { + /* avoid function call for bool */ + for (j = 0; j < count; ++j) { + if (*data != 0) { + *multi_index++ = j; + } + data += stride; + } + } + else { + for (j = 0; j < count; ++j) { + if (nonzero(data, self)) { + *multi_index++ = j; + } + data += stride; } - data += stride; } goto finish; @@ -2630,12 +2641,23 @@ PyArray_Nonzero(PyArrayObject *self) multi_index = (npy_intp *)PyArray_DATA(ret); /* Get the multi-index for each non-zero element */ - do { - if (nonzero(*dataptr, self)) { - get_multi_index(iter, multi_index); - multi_index += ndim; - } - } while(iternext(iter)); + if (PyArray_ISBOOL(self)) { + /* avoid function call for bool */ + do { + if (**dataptr != 0) { + get_multi_index(iter, multi_index); + multi_index += ndim; + } + } while(iternext(iter)); + } + else { + do { + if (nonzero(*dataptr, self)) { + get_multi_index(iter, multi_index); + multi_index += ndim; + } + } while(iternext(iter)); + } } NpyIter_Deallocate(iter); |