In Matlab, the built-in isequal checks to see if two arrays are equal. If they are not equal, this can be very fast, because the implementation seems to stop checking as soon as the difference exists:
>> A = zeros(1e9, 1, 'single'); >> B = A(:); >> B(1) = 1; >> tic; isequal(A, B); toc; Elapsed time is 0.000043 seconds.
Is there any equivalent in Python / numpy? all(A==B) or all(equal(A, B)) much slower as it compares all elements, even if the original is different:
In [13]: A = zeros(1e9, dtype='float32') In [14]: B = A.copy() In [15]: B[0] = 1 In [16]: %timeit all(A==B) 1 loops, best of 3: 612 ms per loop
Is there any numpy equivalent? It should be very easy to implement in C, but slowly implemented in Python, because this is the case when we do not want to translate, so this will require an explicit loop.
Edit:
It seems that array_equal doing what I want. However, this is not faster than all(A==B) because it is not built-in, but simply a short Python function that executes A==B So this does not meet my need for a quick check.
In [12]: %timeit array_equal(A, B) 1 loops, best of 3: 623 ms per loop
source share