From b0e66ad754c2a2efeb8c6f39418e5bb7d3e388c5 Mon Sep 17 00:00:00 2001 From: MattHarrigan Date: Wed, 26 Oct 2016 11:01:05 -0400 Subject: ENH: fast track default kwargs for ediff1d Special case to_begin and to_end both equal to None, avoiding subsequent steps. Particulary faster for small arrays where overhead plays a big role. --- numpy/lib/arraysetops.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index e63e09546..889415efe 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -81,6 +81,10 @@ def ediff1d(ary, to_end=None, to_begin=None): # force a 1d array ary = np.asanyarray(ary).ravel() + # fast track default case + if to_begin is None and to_end is None: + return ary[1:] - ary[:-1] + # get the length of the diff'd values l = len(ary) - 1 if l < 0: -- cgit v1.2.1 From 9a90abf995d0d8d9e96992a083dc55a41a93254f Mon Sep 17 00:00:00 2001 From: MattHarrigan Date: Thu, 27 Oct 2016 19:38:01 -0400 Subject: BUG: return subclasses from ediff1d --- numpy/lib/arraysetops.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 889415efe..836f4583f 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -85,28 +85,27 @@ def ediff1d(ary, to_end=None, to_begin=None): if to_begin is None and to_end is None: return ary[1:] - ary[:-1] - # get the length of the diff'd values - l = len(ary) - 1 - if l < 0: - # force length to be non negative, match previous API - # should this be an warning or deprecated? - l = 0 - if to_begin is None: - to_begin = np.array([]) + l_begin = 0 else: to_begin = np.asanyarray(to_begin).ravel() + l_begin = len(to_begin) if to_end is None: - to_end = np.array([]) + l_end = 0 else: to_end = np.asanyarray(to_end).ravel() + l_end = len(to_end) # do the calculation in place and copy to_begin and to_end - result = np.empty(l + len(to_begin) + len(to_end), dtype=ary.dtype) - result[:len(to_begin)] = to_begin - result[len(to_begin) + l:] = to_end - np.subtract(ary[1:], ary[:-1], result[len(to_begin):len(to_begin) + l]) + l_diff = max(len(ary) - 1, 0) + result = np.empty(l_diff + l_begin + l_end, dtype=ary.dtype) + result = ary.__array_wrap__(result) + if l_begin > 0: + result[:l_begin] = to_begin + if l_end > 0: + result[l_begin + l_diff:] = to_end + np.subtract(ary[1:], ary[:-1], result[l_begin:l_begin + l_diff]) return result -- cgit v1.2.1