title: Why is Numpy outperforming this cython routine by a factor 3
body: <p>I have just started experimenting with cython and as a first exercise I created the following (re)implementation of a function computing the sin for each element of an array. So here's my sin.pyx</p>
  
  <pre><code>from numpy cimport ndarray, float64_t
  import numpy as np
  
  cdef extern from "math.h":
      double sin(double x)
  
  def sin_array(ndarray[float64_t, ndim=1] arr):
      cdef int n = len(arr)
      cdef ndarray h = np.zeros(n, dtype=np.float64)
      for i in range(n):
          h[i] = sin(arr[i])
      return h
  </code></pre>
  
  <p>I also created the following setup.py for this</p>
  
  <pre><code>from distutils.core import setup
  from distutils.extension import Extension
  from Cython.Distutils import build_ext
  
  import numpy
  
  ext = Extension("sin", sources=["sin.pyx"])
  
  setup(ext_modules=[ext],
        cmdclass={"build_ext": build_ext},
        include_dirs=[numpy.get_include()])
  </code></pre>
  
  <p>So this creates my *.so file. I import this into python and create 1000 random numbers, e.g.</p>
  
  <pre><code>import sin
  import numpy as np
  
  x = np.random.randn(1000)
  
  %timeit sin.sin_array(x)
  %timeit np.sin(x)
  </code></pre>
  
  <p>Numpy wins by a factor of 3. Why is that? I thought that a function making very explicit assumption about the type and the dimension of the input-array can be more competitive here. Of course, I also understand that numpy is incredible clever, but chances are that I am doing something stupid here...</p>
  
  <p>Note that the point of this exercise is to not to rewrite a faster sin function but rather to create some cython wrappers for some of our internal tools but that's another issue for later...</p>