title: Cython and numpy speed
body: <p>I'm using cython for a correlation calculation in my python program. I have two audio data sets and I need to know the time difference between them. The second set is cut based on onset times and then slid across the first set. There are two for-loops: one slides the set and the inner loop calculates correlation at that point. This method works very well and it's accurate enough.</p>
  
  <p>The problem is that with pure python this takes more than one minute. With my cython code, it takes about 17 seconds. This still is too much. Do you have any hints how to speed-up this code:</p>
  
  <pre><code>import numpy as np
  cimport numpy as np
  
  cimport cython
  
  FTYPE = np.float
  ctypedef np.float_t FTYPE_t
  
  @cython.boundscheck(False)
  def delay(np.ndarray[FTYPE_t, ndim=1] f, np.ndarray[FTYPE_t, ndim=1] g):
      cdef int size1 = f.shape[0]
      cdef int size2 = g.shape[0]
      cdef int max_correlation = 0
      cdef int delay = 0
      cdef int current_correlation, i, j
  
      # Move second data set frame by frame
      for i in range(0, size1 - size2):
          current_correlation = 0
  
          # Calculate correlation at that point
          for j in range(size2):
              current_correlation += f[&lt;unsigned int&gt;(i+j)] * g[j]
  
          # Check if current correlation is highest so far
          if current_correlation &gt; max_correlation:
              max_correlation = current_correlation
              delay = i
  
      return delay
  </code></pre>