Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize list comprehension with known length #2844

Open
jdemeyer opened this Issue Feb 15, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@jdemeyer
Copy link
Contributor

jdemeyer commented Feb 15, 2019

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.

@scoder

This comment has been minimized.

Copy link
Contributor

scoder commented Feb 15, 2019

Also see #1311.

@scoder

This comment has been minimized.

Copy link
Contributor

scoder commented Feb 15, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.