Why is a.insert(0,0) much slower than a[0:0]=[0]?
Solution 1:
I think it's probably just that they forgot to use memmove
in list.insert
. If you take a look at the code list.insert
uses to shift elements, you can see it's just a manual loop:
for (i = n; --i >= where; )
items[i+1] = items[i];
while list.__setitem__
on the slice assignment path uses memmove
:
memmove(&item[ihigh+d], &item[ihigh],
(k - ihigh)*sizeof(PyObject *));
memmove
typically has a lot of optimization put into it, such as taking advantage of SSE/AVX instructions.