I wrote a simple cython script to optimize the collections.Counter of a dictionary counter and the python zip implementation (the main input is a list of tuples). Is there a way to speed it up?%%cython --annotatecimport cythonimport numpy as npcimport numpy as npfrom collections import defaultdict@cython.boundscheck(False)@cython.wraparound(False)def uniqueCounterListCython(list x not None):    cdef:        Py_ssize_t i,n      n = len(x)        dx = defaultdict(int)    for i from 0 <= i < n:        dx[x[i]] += 1    return dx@cython.boundscheck(False)@cython.wraparound(False)def zipCython(np.ndarray[long,ndim=1] x1 not None, np.ndarray[long,ndim=1] x2 not None):    cdef:        Py_ssize_t i,n    n = x1.shape[0]    l=[]    for i from 0 <= i < n:        l.append(((x1[i],x2[i])))    return lSample input - uniqueCounterListCython(zipCython(np.random.randint(0,3,200000),np.random.randint(0,3,200000)))EDIT:Found a kind of trivial way to speed things up - just merge the two functions:@cython.boundscheck(False)@cython.wraparound(False)def uniqueCounterListCythonWithZip(np.ndarray[long,ndim=1] x1 not None, np.ndarray[long,ndim=1] x2 not None):    cdef:        Py_ssize_t i,n      n = x1.shape[0]        dx = defaultdict(int)    for i from 0 <= i < n:        dx[((x1[i],x2[i]))] += 1    return dxAny more suggestions? 
Speedup cython dictionary counter
python;optimization;numpy;cython
You don't give us much context for this problem, so it's unclear to me exactly what you are trying to achieve. But in your example, you have a pair of NumPy arrays containing integers in the range 02, and you seem to want to count the number of occurrences of each pair of values.So I suggest encoding pairs of integers in the range 02 into a single integer in the range 08, using numpy.bincount to do the counting, and then using numpy.reshape to decode the result, like this:>>> import numpy as np>>> x, y = np.random.randint(0,3,200000), np.random.randint(0,3,200000)>>> counts = np.bincount(x * 3 + y).reshape((3, 3))>>> countsarray([[22282, 22093, 22247],       [22084, 22295, 22396],       [22012, 22243, 22348]])A quick check that I got the encoding/decoding right:>>> counts[0,2] == np.count_nonzero((x == 0) & (y == 2))TrueThis runs much faster than the code in your question (assuming I have interpreted your profile screenshots correctly):>>> from timeit import timeit>>> timeit(lambda:np.bincount(x * 3 + y).reshape((3, 3)), number=1000)2.7519797360000666