[Cython] Code generated for the expression int(x)+1

Ask F. Jakobsen ask at linet.dk
Tue May 1 09:53:17 CEST 2012


Hi all,

I am having a simple performance problem that can be resolved by splitting up an expression in two lines. I don't know if it is a bug or I am missing something.

The piece of code below is translated to slow code

1)
    cdef int i
    i=int(x)+1

whereas the code below is translated to fast code

2)
    cdef int i
    i=int(x)
    i=i+1

Snippet of generated code by cython

1)


  /* "test.pyx":4
 *     cdef double x=3.2
 *     cdef int i
 *     i=int(x)+1             # <<<<<<<<<<<<<<
 *     return i
 * 
 */
  __pyx_t_1 = PyFloat_FromDouble(__pyx_v_x); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)(&PyInt_Type))), ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_3 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_3 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_v_i = __pyx_t_3;

2)

  /* "test.pyx":11
 *     cdef double x=3.2
 *     cdef int i
 *     i=int(x)             # <<<<<<<<<<<<<<
 *     i=i+1
 *     return i
 */
  __pyx_v_i = ((int)__pyx_v_x);

  /* "test.pyx":12
 *     cdef int i
 *     i=int(x)
 *     i=i+1             # <<<<<<<<<<<<<<
 *     return i
 * 
 */
  __pyx_v_i = (__pyx_v_i + 1);


I am using Cython-0.15.1

Best regards,
              Ask


More information about the cython-devel mailing list