Skip to content

Optimization: Replace (~accepted).sum() with size - count_nonzero for efficient False counting #1

@SaFE-APIOpt

Description

@SaFE-APIOpt

n_reject = (~accepted).sum()

Hi, thank you for your great work!

In the following line:
n_reject = (~accepted).sum()
the goal is to count the number of False values in the boolean array accepted. While this is functionally correct, the use of ~accepted followed by .sum() introduces performance overhead that can be avoided.

At the implementation level, ~accepted creates a new temporary array by applying a bitwise NOT operation element-wise. This results in a complete copy of the original array with all values inverted (True → False, False → True). Then .sum() performs another full traversal of this new array, converting each boolean to an integer and accumulating the result.

A more efficient alternative is to use:
n_reject = np.size(accepted) - np.count_nonzero(accepted)
This avoids creating intermediate arrays and takes advantage of NumPy's optimized C-level count_nonzero function, which performs the count in a single pass with minimal memory usage.

Replacing (~accepted).sum() with this version improves both speed and memory efficiency, especially when working with large boolean arrays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions