Simon King
unread,Nov 14, 2011, 5:54:19 PM11/14/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sage-devel
Hi!
I am sure that the following problem has a solution that is well-known
- but not well-known to me, and also I did not succeed in googling it.
Background is #12029.
In the following, self is a ClonableIntArray, but I guess the problem
is more general. self._list is some "int *" array of known length
self._len. The aim is to convert self._list into a Python list, as
quickly as possible.
Is there a ready-made function that would do such conversion?
I did this, which works fine:
cdef int i
cdef list L = []
for i from 0<=i<self._len:
L.append(self._list[i])
return L
But Florent was suggesting that it might be faster to create an
"empty" list of length self._len using PyList_New, and to put the
items onto that list by PyList_SetItem, rather than appending them.
First question: Could PyList_SET_ITEM (for a list of known length)
really be faster than appending to the list?
Second question: I know that PyList_SET_ITEM is only borrowing a
reference. Hence, I am not surprised that I got segfaults in the first
place. But why am I still getting segfaults with the following code,
where I manually add a reference?
cdef int i, o
cdef list L = PyList_New(self._len)
for i from 0<=i<self._len:
o = self._list[i]
Py_INCREF(o)
PyList_SET_ITEM(L,i,o)
return L
More precisely, it works (or seems to work) on ClonableIntArrays of
size 100, but not on a list of size 500.
Best regards,
Simon