[Numpy-discussion] all elements equal
Neal Becker
ndbecker2@gmail....
Mon Mar 5 14:06:23 CST 2012
Keith Goodman wrote:
> On Mon, Mar 5, 2012 at 11:52 AM, Benjamin Root <ben.root@ou.edu> wrote:
>> Another issue to watch out for is if the array is empty. Technically
>> speaking, that should be True, but some of the solutions offered so far
>> would fail in this case.
>
> Good point.
>
> For fun, here's the speed of a simple cython allclose:
>
> I[2] a = np.ones(100000)
> I[3] timeit a.min() == a.max()
> 10000 loops, best of 3: 106 us per loop
> I[4] timeit allequal(a)
> 10000 loops, best of 3: 68.9 us per loop
>
> I[5] a[1] = 9
> I[6] timeit a.min() == a.max()
> 10000 loops, best of 3: 102 us per loop
> I[7] timeit allequal(a)
> 1000000 loops, best of 3: 269 ns per loop
>
> where
>
> @cython.boundscheck(False)
> @cython.wraparound(False)
> def allequal(np.ndarray[np.float64_t, ndim=1] a):
> cdef:
> np.float64_t a0
> Py_ssize_t i, n=a.size
> a0 = a[0]
> for i in range(n):
> if a[i] != a0:
> return False
> return True
But doesn't this one fail on empty array?
More information about the NumPy-Discussion
mailing list