title: Declaring numpy array and c pointer in cython
body: <p>In my code I usally use numpy arrays to interface between methods and classes. Optimizing the core parts of my program I use cython with c pointers of those numpy arrays. Unforunately, the way I'm currently declaring the arrays is quite long.</p>
  
  <p>For example, let's say I have a method which should return a numpy array someArrayNumpy, but inside the function pointers *someArrayPointers should be used for speed. This is how I usually declare this:</p>
  
  <pre><code>cdef:
      numpy.ndarray someArrayNumpy = numpy.zeros(someArraySize)
      numpy.ndarray[numpy.double_t, ndim=1] someArrayBuff = someArrayNumpy
      double *someArrayPointers = &lt;double *&gt; someArrayBuff.data
  
  [... some Code ...]
  
  return someArrayNumpy
  </code></pre>
  
  <p>As you can see, this takes up 3 lines of code for basically one array, and often I have to declare more of those arrays.</p>
  
  <p>Is there a more compact/clever way to do this? I think I am missing something.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So because it was asked by J. Martinot-Lagarde I timed C pointers and "numpy pointers". The code was basically</p>
  
  <pre><code>for ii in range(someArraySize):
      someArrayPointers[ii] += 1
  </code></pre>
  
  <p>and</p>
  
  <pre><code>for ii in range(someArraySize):
      someArrayBuff[ii] += 1
  </code></pre>
  
  <p>with the definitions from above, but I added "ndim=1, mode='c'" just to make sure. Results are for someArraySize = 1e8 (time in ms):</p>
  
  <pre><code>testMartinot("cPointers")
  531.276941299
  testMartinot("numpyPointers")
  498.730182648
  </code></pre>
  
  <p>That's what I roughly remember from previous/different benchmarks. </p>