Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH: Improve performance of numpy.nonzero for 1D/2D contiguous arrays #27519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into nonzero_optmize
  • Loading branch information
eendebakpt committed Feb 23, 2025
commit 5444f08274c3f2a2a0e56a28c78ede84105a34db
9 changes: 6 additions & 3 deletions 9 numpy/_core/src/multiarray/item_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2810,10 +2810,12 @@ nonzero_idxs_1D_bool(npy_intp count, npy_intp nonzero_count, char *data,
* the fast bool count is followed by this sparse path is faster
* than combining the two loops, even for larger arrays
*/
npy_intp * multi_index_end = multi_index + nonzero_count;

if (((double)nonzero_count / count) <= 0.1) {
npy_intp subsize;
npy_intp j = 0;
while (1) {
while (multi_index < multi_index_end) {
npy_memchr(data + j * stride, 0, stride, count - j,
&subsize, 1);
j += subsize;
Expand All @@ -2832,7 +2834,7 @@ nonzero_idxs_1D_bool(npy_intp count, npy_intp nonzero_count, char *data,
npy_intp j = 0;

/* Manually unroll for GCC and maybe other compilers */
while (multi_index + 4 < multi_index_end) {
while (multi_index + 4 < multi_index_end && (j < count - 4) ) {
*multi_index = j;
multi_index += data[0] != 0;
*multi_index = j + 1;
Expand All @@ -2845,7 +2847,7 @@ nonzero_idxs_1D_bool(npy_intp count, npy_intp nonzero_count, char *data,
j += 4;
}

while (multi_index < multi_index_end) {
while (multi_index < multi_index_end && (j < count) ) {
*multi_index = j;
multi_index += *data != 0;
data += stride;
Expand Down Expand Up @@ -2956,6 +2958,7 @@ PyArray_Nonzero(PyArrayObject *self)
for (npy_intp j = 0; j < count; ++j) {
if (nonzero(data, self)) {
if (++added_count > nonzero_count) {

break;
}
*multi_index++ = j;
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.