sorting many arrays from one...

Jonathan Hogg jonathan at onegoodidea.com
Sun Jul 14 13:11:42 EDT 2002


On 14/7/2002 6:46, in article
mailman.1026625704.19305.python-list at python.org, "Tim Peters"
<tim.one at comcast.net> wrote:

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

Oh well, in that case you can trim another couple of % off the calls with:

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.116
diff -r2.116 listobject.c
783c783
<       res = PyEval_CallObject(compare, args);
---
>       res = PyObject_Call(compare, args, NULL);

Since PyEval_CallObject just does some unnecessary typechecks on the
arguments. Or being even more aggressive:

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.116
diff -r2.116 listobject.c
783c783
<       res = PyEval_CallObject(compare, args);
---
>       res = (*compare->ob_type->tp_call)(compare, args, NULL);
1297a1298,1302
>       }
>       if (compare && compare->ob_type->tp_call == NULL) {
>               PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
>                            compare->ob_type->tp_name);
>               return NULL;

i.e., pull the remaining typecheck out to 'listsort'. But I don't much like
this one as it gains only a tiny amount over the one above, seems quite a
bit more obtuse (or is there a neater callable check macro?), and also
subtly changes the semantics (sorting an empty list with a non-callable
fails - though perhaps it should).

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

Yeah, I've had a short wander through the innards of cmp. It's a bit of a
convoluted one, but necessarily so. I don't see how the calls can be made
more optimal and cmp has to be the way it is, so I don't think '.sort(cmp)'
can be made any faster without checking to see if 'compare' *is* 'cmp' and
then not bothering to make the calls ;-)

Jonathan




More information about the Python-list mailing list