由于您使用numpy,因此我怀疑bisec根本不会对您有所帮助。因此,我建议您做两件事:
- 千万 不能 使用
np.sort
,使用c.sort()
方法,而不是这种种取代阵列,避免了复制。 np.unique
必须使用np.sort
没有到位的。因此,np.unique
不要手动使用逻辑。IE浏览器 首先进行排序(就地),然后np.unique
手动执行方法(还检查其python代码),并flag = np.concatenate(([True], ar[1:] != ar[:-1]))
与之一起使用unique = ar[flag]
(对ar进行排序)。为了更好一点,您可能应该使标志操作本身就位。flag = np.ones(len(ar), dtype=bool)
然后np.not_equal(ar[1:], ar[:-1], out=flag[1:])
基本上避免了的完整副本flag
。- 我对此不确定。但是
.sort
有3种不同的算法,因为您的数组可能已经差不多排序了,所以更改排序方法可能会产生速度差异。
这将使完整的东西接近您所得到的(无需事先进行独特的处理):
def insort(a, b, kind='mergesort'): # took mergesort as it seemed a tiny bit faster for my sorted large array try. c = np.concatenate((a, b)) # we still need to do this unfortunatly. c.sort(kind=kind) flag = np.ones(len(c), dtype=bool) np.not_equal(c[1:], c[:-1], out=flag[1:]) return c[flag]



