Skip to content

Commit 8197e2b

Browse files
committed
ENH: Convert to list before appending and zipping
It's faster for arrays of less than 10000 indices.
1 parent 9be87aa commit 8197e2b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/matplotlib/mlab.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,14 +3884,14 @@ def contiguous_regions(mask):
38843884
idx, = np.nonzero(mask[:-1] != mask[1:])
38853885
idx += 1
38863886

3887+
# List operations are faster for moderately sized arrays
3888+
idx = idx.tolist()
3889+
38873890
# Add first and/or last index if needed
3888-
if mask[0] or mask[-1]:
3889-
idx = (idx,)
3890-
if mask[0]:
3891-
idx = ([0],) + idx
3892-
if mask[-1]:
3893-
idx = idx + ([len(mask)],)
3894-
idx = np.concatenate(idx)
3891+
if mask[0]:
3892+
idx = [0] + idx
3893+
if mask[-1]:
3894+
idx.append(len(mask))
38953895

38963896
return list(zip(idx[::2], idx[1::2]))
38973897

0 commit comments

Comments
 (0)