diff options
author | Stephan Hoyer <shoyer@gmail.com> | 2016-10-24 11:12:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-24 11:12:34 -0400 |
commit | 3bd79ab568856678139fa4d7ca272e7cbd05e1ad (patch) | |
tree | 6ad12614b5222ef9f22b5785c4fda1c8d080c8b1 /numpy/lib/arraysetops.py | |
parent | e7d6f36de23061fe3b1f3c1aabe273fc4999acc4 (diff) | |
parent | 484827189756d45e6231bfd2cd3cf6db9c2993df (diff) | |
download | numpy-3bd79ab568856678139fa4d7ca272e7cbd05e1ad.tar.gz |
Merge pull request #8183 from mattharrigan/ediff1d-performance
Ediff1d performance
Diffstat (limited to 'numpy/lib/arraysetops.py')
-rw-r--r-- | numpy/lib/arraysetops.py | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 17dfa7567..e63e09546 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -78,20 +78,33 @@ def ediff1d(ary, to_end=None, to_begin=None): array([ 1, 2, -3, 5, 18]) """ - ary = np.asanyarray(ary).flat - ed = ary[1:] - ary[:-1] - arrays = [ed] - if to_begin is not None: - arrays.insert(0, to_begin) - if to_end is not None: - arrays.append(to_end) - - if len(arrays) != 1: - # We'll save ourselves a copy of a potentially large array in - # the common case where neither to_begin or to_end was given. - ed = np.hstack(arrays) - - return ed + # force a 1d array + ary = np.asanyarray(ary).ravel() + + # 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([]) + else: + to_begin = np.asanyarray(to_begin).ravel() + + if to_end is None: + to_end = np.array([]) + else: + to_end = np.asanyarray(to_end).ravel() + + # 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]) + return result + def unique(ar, return_index=False, return_inverse=False, return_counts=False): """ |