sorting many arrays from one...

Tim Peters tim.one at comcast.net
Sun Jul 14 01:46:56 EDT 2002


[Jonathan Hogg]
> Out of curiosity I took a look at the code and I could only think of one
> optimisation:
>
> Index: listobject.c
> ===================================================================
> RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
> retrieving revision 2.114
> diff -r2.114 listobject.c
> 775c775
> <       args = Py_BuildValue("(OO)", x, y);
> ---
> >       args = PyTuple_New(2);
> 777a778,781
> >       PyTuple_SetItem(args, 0, x);
> >       Py_INCREF(x);
> >       PyTuple_SetItem(args, 1, y);
> >       Py_INCREF(y);
>
>
> This takes the difference between '.sort()' and '.sort(cmp)' for a list
> of integers down from 6.5x slower to 4.5x slower on my machine
> (300,000 element list). But the improvement is fairly marginal in my book
> since, unless your comparison function is a C function, invoking the
> bytecode interpreter makes it at least 10x slower (12x slower without
> the above change).

I've worked very much harder for very much less gain.  Thanks!  I checked in
a variant of this (inside the core it's kosher to use the quicker
PyTuple_SET_ITEM macro).

> Beyond that I couldn't see much in the way of obvious changes
> that could be made. It just fundamentally takes a non-zero amount
> of time to make a PyObject_Call and if you make 5 million of them
> in rapid succession then it adds up.

It also adds up even if you do 5 million in slow succession <wink>.  If your
fingers are well-rested, get into a debugger and single-step through what
happens when cmp gets called in this context.  It's a revelation of
apocalyptic dimension <wink>.






More information about the Python-list mailing list