-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Line 76 in 1fa2138
| 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.