Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython: for i from 1 <= i < N

I'm learning Cython and came across this snippit of code:

import numpy as np
cimport numpy as np

def mean(np.ndarray[np.double_t] input):

    cdef np.double_t cur
    # Py_ssize_t is numpy's index type
    cdef Py_ssize_t i
    cdef Py_ssize_t N = len(input)

    for i from 0 <= i < N:
        cur += input[i]

    return cur / N

a=np.array([1,2,3,4], dtype=np.double)

Obviously, this returns the mean of a which is 2.5. My question is this:

Is the for loop a Python loop, Cython, or C?

like image 643
Jason Strimpel Avatar asked Nov 10 '11 17:11

Jason Strimpel


2 Answers

Compile it and see: the C code that Cython produces is nicely annotated.

  /* "cyexample.pyx":11
 *     cdef Py_ssize_t N = len(input)
 * 
 *     for i from 0 <= i < N:             # <<<<<<<<<<<<<<
 *         cur += input[i]
 * 
 */
  __pyx_t_1 = __pyx_v_N;
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_1; __pyx_v_i++) {
    /* "cyexample.pyx":12
 * 
 *     for i from 0 <= i < N:
 *         cur += input[i]             # <<<<<<<<<<<<<<
 * 
 *     return cur / N
 */
    __pyx_t_2 = __pyx_v_i;
    __pyx_t_3 = -1;
    if (__pyx_t_2 < 0) {
      __pyx_t_2 += __pyx_bshape_0_input;
      if (unlikely(__pyx_t_2 < 0)) __pyx_t_3 = 0;
    } else if (unlikely(__pyx_t_2 >= __pyx_bshape_0_input)) __pyx_t_3 = 0;
    if (unlikely(__pyx_t_3 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_3);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_v_cur = (__pyx_v_cur + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_double_t *, __pyx_bstruct_input.buf, __pyx_t_2, __pyx_bstride_0_input)));
  }

And so the loop itself is successfully turned into C. Note that these days Cython can handle range naturally, so the older "from 0 <= i < N" style isn't necessary. The point of introducing the (non-Python) "for/from" syntax was to signify which loops should be C-ified.

like image 170
DSM Avatar answered Oct 23 '22 23:10

DSM


for..from seems to be a Pyrex / Cython loop: http://docs.cython.org/src/userguide/language_basics.html#integer-for-loops

like image 42
millimoose Avatar answered Oct 23 '22 22:10

millimoose