how not use memmove when insert a object in the list

K

kyo guan

Hi :

python list object like a stl vector, if insert a object in the front or the middle of it,
all the object after the insert point need to move backward.

look at this code ( in python 2.4.3)

static int
ins1(PyListObject *self, int where, PyObject *v)
{
int i, n = self->ob_size;
PyObject **items;
if (v == NULL) {
PyErr_BadInternalCall();
return -1;
}
if (n == INT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"cannot add more objects to list");
return -1;
}

if (list_resize(self, n+1) == -1)
return -1;

if (where < 0) {
where += n;
if (where < 0)
where = 0;
}
if (where > n)
where = n;
items = self->ob_item;
for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
items[i+1] = items;
Py_INCREF(v);
items[where] = v;
return 0;
}



Kyo.
 
J

John Machin

Hi :

python list object like a stl vector, if insert a object in the front or the middle of it,
all the object after the insert point need to move backward.

look at this code ( in python 2.4.3)
for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
items[i+1] = items;


Here's a guess, based on similar work on another language a few
reincarnations ago :)

memmove() is very general-purpose, and starts with byte addresses and a
byte count. For a small number of list elements, by the time that
memmove has determined (1) the move overlaps (2) both source and target
are on word boundaries and it is moving a whole number of words (3) what
direction (up or down), the DIY code has already finished. For a large
number of items, memmove *may* be faster (depending on the architecture
and the compiler) but you are using the wrong data structure anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top