HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Linspace method build in Python's C API

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
methodlinspacepythonapibuild

Problem

Today I built a linspace function in Python's C API:

static PyObject *
linspace(PyObject * self, PyObject * args)
{
    int n, i;
    double start, end;

    if (!PyArg_ParseTuple(args, "ddi:linspace", &start, &end, &n))
        return NULL;

    if (n <= 1)
        return Py_BuildValue("[d]", end);

    double h;
    PyObject *pylist = PyList_New(n);

    h = (end - start) / (n - 1);

    for (i = 0; i < n; i++)
        PyList_SetItem(pylist, i, Py_BuildValue("d", (start + h * i)));

    return Py_BuildValue("O", pylist);
}


It behaves how I would like it to behave, however, when I benchmarked it against NumPy's linspace it was slower by about a factor of 80.

I have a few questions that I think may be affecting performance, but I can't seem to find help online:

  • Is there a memory leak? Or am I not incrementing or decrementing any references that I should be?



  • Can I do this with a C double array and then return that as a Python Object? Would this even be faster (I think it may)?



  • Am I missing something? I am new to the C API and I am not confident in it yet.

Solution

-
I can't see any obvious memory leaks. If you're worried, then you might start out by seeing what sys.getrefcount tells you.

-
You will need to package up your array-of-doubles as a new type of Python object. See section 2 of the Extending/Embedding manual.

-
Since you know that you are creating float objects, you could speed things up slightly by using PyFloat_FromDouble instead of the generic Py_BuildValue (which has to parse its first argument and then dispatch). But this is not going to beat NumPy, because packaging up an array-of-numbers as a new type of Python object is exactly what NumPy does, and that's why it runs so fast: it doesn't have to allocate a new Python object for every position in the list.

Context

StackExchange Code Review Q#15465, answer score: 3

Revisions (0)

No revisions yet.