Re: how not use memmove when insert a object in the list



On 30/04/2006 11:57 AM, kyo guan wrote:
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[i];

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.
.