On my machine, this is faster:
(a == b).sum()
If you do not want to use any additional storage, I would suggest using numba. I am not too familiar with this, but it works well. I ran into some problems that caused Cython to take a NumPy boolean array.
from numba import autojit def pysumeq(a, b): tot = 0 for i in xrange(a.shape[0]): for j in xrange(a.shape[1]): if a[i,j] == b[i,j]: tot += 1 return tot
If you don't have numba, I would suggest using @ user2357112 answer
Edit: just got a version of Cython, here is the .pyx file. I would go with that.
from numpy cimport ndarray as ar cimport numpy as np cimport cython @cython.boundscheck(False) @cython.wraparound(False) def cysumeq(ar[np.uint8_t,ndim=2,cast=True] a, ar[np.uint8_t,ndim=2,cast=True] b): cdef int i, j, h=a.shape[0], w=a.shape[1], tot=0 for i in xrange(h): for j in xrange(w): if a[i,j] == b[i,j]: tot += 1 return tot
source share