cython icon indicating copy to clipboard operation
cython copied to clipboard

Optimize list comprehension with known length

Open jdemeyer opened this issue 6 years ago • 2 comments

Many list comprehensions are of the form

[FOO for i in range(n)]

but Cython does not take advantage of the fact that the length is known to be n in advance. In this example

from cpython.object cimport PyObject

cdef extern from "Python.h":
    void Py_INCREF(PyObject *)
    PyObject * PyInt_FromLong(long ival)
    list PyList_New(Py_ssize_t size)
    void PyList_SET_ITEM(list l, Py_ssize_t, PyObject *)

def listcomp1(long n):
    cdef long i
    return [i*i for i in range(n)]

def listcomp2(long n):
    cdef long i
    cdef list T = PyList_New(n)
    for i in range(n):
        PyList_SET_ITEM(T, i, PyInt_FromLong(i*i))
    return T

the second variant can be 25% faster for a moderately-sized list.

jdemeyer avatar Feb 15 '19 08:02 jdemeyer

Also see #1311.

scoder avatar Feb 15 '19 08:02 scoder

Might not be entirely trivial due to refcount handling of the values.

scoder avatar Feb 15 '19 09:02 scoder