[Python-Dev] Comparison speed

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Mon, 21 May 2001 19:03:59 +0200


> Note that the usual way to write this is
> 
>  		if (c < 0 && PyErr_Occurred())
> 
> More work for my artificial "ab" < "cd" case but a net win in real life (when
> c >= 0, it's an internal error if PyErr_Occurred() were to return true; alas,
> when c < 0 there's no way in the cmp protocol to use c's value alone to
> distinguish between "less than" and "error").

Ok. I've updated my tp_compare patch on SF to do so; it also
un-deprecates UserList.__cmp__.

> > Here, I get 3 function calls: f is string_compare, then
> > PyErr_Occurred, finally convert_3way_to_object, which converts
> > {-1,0,1} x Op -> {Py_True, Py_False}.
> 
> Unfortunately, it's 4 calls, as PyErr_Occurred() isn't a leaf.

Any reason why PyThreadState_GET isn't used there?

> There's no danger of over-indexing when ob_size==0, because it doesn't
> include the trailing null byte Python always sticks at the end of string
> objects; and the first-byte check is much more likely to pay off than the
> zero-length check (comparison to a null string?  gotta be rare as clear
> conclusions <wink>), and better to test for the more common case first.

This is now also in the string_richcompare patch on SF.

Regards,
Martin