[Python-Dev] Comparison speed

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Thu, 17 May 2001 21:45:29 +0200


> 1. Code using cmp(string1, string2) will clearly run significantly
>    slower, calling string comparison 1 (when == obtains), 2 (when <
>    obtains), or 3 (when > obtains) times instead of always once only.

I'd like to question the rationale behind this procedure. If a type
has both tp_compare and tp_richcompare, and the application is
performing cmp(o1, o2): Why is it then a good thing to emulate 3way
compare using rich compare?

I just changed the order in do_cmp, to the IMO more correct 

	if (v->ob_type == w->ob_type
	    && (f = v->ob_type->tp_compare) != NULL)
		return (*f)(v, w);
	c = try_rich_to_3way_compare(v, w);
	if (c < 2)
		return c;
	c = try_3way_compare(v, w);
	if (c < 2)
		return c;
	return default_3way_compare(v, w);

With that, I got only a single failure in the test suite:
test_userlist fails with

exceptions.RuntimeError: UserList.__cmp__() is obsolete

Tim thinks this is a bug in UserList, since __cmp__ is not obsolete; I
agree.

According to the CVS log, this implementation of do_cmp was installed
in object.c 2.105, by gvanrossum, on 2001/01/17. What was the specific
rationale for doing do_cmp in that order?

Regards,
Martin