Skip to content

Commit

Permalink
MAINT: Make setting mask values with scalars faster.
Browse files Browse the repository at this point in the history
The current implementation assigns the mask value to a.flat, where a is
the mask being updated. In the scalar case, that is slower than
assigning to a[...], so when a is an instance of int, float, np.bool_,
or np.number use that instead.
  • Loading branch information
rhattersley authored and charris committed Aug 17, 2013
1 parent aeb13af commit 0cc113c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3129,6 +3129,11 @@ def __setmask__(self, mask, copy=False):
if self._hardmask:
current_mask |= mask
# Softmask: set everything to False
# If it's obviously a compatible scalar, use a quick update
# method...
elif isinstance(mask, (int, float, np.bool_, np.number)):
current_mask[...] = mask
# ...otherwise fall back to the slower, general purpose way.
else:
current_mask.flat = mask
# Named fields w/ ............
Expand Down Expand Up @@ -3158,6 +3163,11 @@ def __setmask__(self, mask, copy=False):
for n in idtype.names:
current_mask[n] |= mask[n]
# Softmask: set everything to False
# If it's obviously a compatible scalar, use a quick update
# method...
elif isinstance(mask, (int, float, np.bool_, np.number)):
current_mask[...] = mask
# ...otherwise fall back to the slower, general purpose way.
else:
current_mask.flat = mask
# Reshape if needed
Expand Down

0 comments on commit 0cc113c

Please sign in to comment.