Cython_book Chapter 2: Example fib()


In [1]:
%load_ext Cython

In [2]:
%%cython -a
def fib(int n):
    cdef int i
    cdef double a=0.0, b=1.0
    for i in range(n):
        a, b = b, a+b
    return a


Out[2]:
Cython: _cython_magic_299d5c5cdd10498b446c075612702146.pyx

Generated by Cython 0.25.2

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+1: def fib(int n):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_299d5c5cdd10498b446c075612702146_1fib(PyObject *__pyx_self, PyObject *__pyx_arg_n); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_299d5c5cdd10498b446c075612702146_1fib = {"fib", (PyCFunction)__pyx_pw_46_cython_magic_299d5c5cdd10498b446c075612702146_1fib, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_299d5c5cdd10498b446c075612702146_1fib(PyObject *__pyx_self, PyObject *__pyx_arg_n) {
  int __pyx_v_n;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("fib (wrapper)", 0);
  assert(__pyx_arg_n); {
    __pyx_v_n = __Pyx_PyInt_As_int(__pyx_arg_n); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 1, __pyx_L3_error)
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L3_error:;
  __Pyx_AddTraceback("_cython_magic_299d5c5cdd10498b446c075612702146.fib", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_46_cython_magic_299d5c5cdd10498b446c075612702146_fib(__pyx_self, ((int)__pyx_v_n));

  /* function exit code */
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_299d5c5cdd10498b446c075612702146_fib(CYTHON_UNUSED PyObject *__pyx_self, int __pyx_v_n) {
  CYTHON_UNUSED int __pyx_v_i;
  double __pyx_v_a;
  double __pyx_v_b;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("fib", 0);
/* … */
  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_AddTraceback("_cython_magic_299d5c5cdd10498b446c075612702146.fib", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
/* … */
  __pyx_tuple_ = PyTuple_Pack(5, __pyx_n_s_n, __pyx_n_s_n, __pyx_n_s_i, __pyx_n_s_a, __pyx_n_s_b); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 1, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_tuple_);
  __Pyx_GIVEREF(__pyx_tuple_);
/* … */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_299d5c5cdd10498b446c075612702146_1fib, NULL, __pyx_n_s_cython_magic_299d5c5cdd10498b44); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_fib, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 2:     cdef int i
+3:     cdef double a=0.0, b=1.0
  __pyx_v_a = 0.0;
  __pyx_v_b = 1.0;
+4:     for i in range(n):
  __pyx_t_1 = __pyx_v_n;
  for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) {
    __pyx_v_i = __pyx_t_2;
+5:         a, b = b, a+b
    __pyx_t_3 = __pyx_v_b;
    __pyx_t_4 = (__pyx_v_a + __pyx_v_b);
    __pyx_v_a = __pyx_t_3;
    __pyx_v_b = __pyx_t_4;
  }
+6:     return a
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_5 = PyFloat_FromDouble(__pyx_v_a); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 6, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_r = __pyx_t_5;
  __pyx_t_5 = 0;
  goto __pyx_L0;

In [3]:
%timeit -n3 -r3 fib(500)


3 loops, best of 3: 566 ns per loop

In [ ]: