[Cython] can we deprecate for-from loops?

Robert Bradshaw robertwb at math.washington.edu
Thu Oct 15 19:51:42 EDT 2015


On Thu, Oct 15, 2015 at 2:26 PM, Jakub Wilk <jwilk at jwilk.net> wrote:
> * Stefan Behnel <stefan_ml at behnel.de>, 2015-10-11, 16:30:
>>
>> The syntax construct "for i from 0 <= i < 10" has been silently outdated
>> for years. Can we start issuing a warning that normal range() loops are
>> preferred?
>
>
> Hmm. AFAICS, Cython doesn't aways optimize range() loops that well... For
> the attached example code, Cython 0.23.4 generated nice for loops for the
> for_from() function:
>
>  for (__pyx_t_1 = 0; __pyx_t_1 < 10; __pyx_t_1++) {
>    ...
>    for (__pyx_t_3 = 0; __pyx_t_3 < 10; __pyx_t_3++) {
>    ...
>
> but not for the for_range() function:
>
>  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_tuple_, NULL);
> ...
>  ...
>
> (The problem goes away if cdef the iteration variables, which I should do
> anyway, so it's not a big deal for me.)

Correct, if you don't cdef the iteration (or bounds) variable it
doesn't know what size of int to use (e.g. you could be doing
range(10**100, 10**100 + 10). Also, as you point out, the overhead of
using a Python target variable (for either format) is larger than
range vs. C for loop.


More information about the cython-devel mailing list