[Python-Dev] Comparison speed (original) (raw)

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 && PyErrOccurred())_ _More work for my artificial "ab" < "cd" case but a net win in real life (when_ _c >= 0, it's an internal error if PyErrOccurred() 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 stringcompare, then > PyErrOccurred, finally convert3waytoobject, which converts > {-1,0,1} x Op -> {PyTrue, PyFalse}.

Unfortunately, it's 4 calls, as PyErrOccurred() isn't a leaf.

Any reason why PyThreadState_GET isn't used there?

There's no danger of over-indexing when obsize==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 ), and better to test for the more common case first.

This is now also in the string_richcompare patch on SF.

Regards, Martin