Re: newbie list processing question
Greg Ewing <[email protected]>
| Newsgroups | gmane.comp.python.pyrex |
|---|---|
| Message-ID | <[email protected]> |
Daniel Ashbrook wrote: > I've been trying to figure it out using > PyList_GetItem/SetItem and so on, but it seems extremely convoluted, like: > > PyList_SetItem(l,i,PyInt_FromLong(PyInt_AsLong(PyList_GetItem(l,i))+1)) There's no need for it to be *that* convoluted! Pyrex will take care of all the conversions between C and Python ints, so you can just write PyList_SetItem(l, i, PyList_GetItem(l, i) + 1) That will actually do the addition as a Python addition, but that's probably faster than converting to a C int and back anyway. To answer your main question, currently, calling the Python/C API directly is the only way to do any better than what Pyrex naturally generates. In the next release, though, you'll be able to declare things as lists and have some of this done automatically. Don't expect to get any spectacular improvements from this, however. Depending on what you're doing, the slowest part is probably dealing with the Python ints. If you're doing a lot of arithmetic on the list items, you may be better off converting the whole list to an array of C ints, operating on that, and converting the result back to a Python list. In other words, there are no magic techniques to make processing Python lists blazingly fast. The fact that they're Python lists of Python objects puts some fundamental limits on what can be achieved. > (Plus that line doesn't work - trying to > use "for i from" gives me errors about making i an integer from a > pointer without a cast.) Which line? There was no "for i from" anywhere in the code you included. -- Greg