[Python-Dev] Comparison speed

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Wed, 16 May 2001 09:24:56 +0200


>     GvR> I'm curious where the frequent comparisons of types come
>     GvR> from.
> 
> Not to mention the several hundred comparisons to None.

This is harder to analyse; I set a gdb breakpoint on the place where
RichCompare gets PyType_Type, then tried to see what it does, then
ignoring the breakpoint a few times. This is what I've found; I may
miss important cases.

In PyXML, the expression

   type(e) in [types.StringType, types.UnicodeType]

is frequently computed. This is a sequence_contains, which in turn does two
Py_EQ tests. In addition, compile.c:com_add has

   t = Py_BuildValue("(OO)", v, v->ob_type)
   PyDict_GetItem(dict, t)

Again, the dictionary lookup performs Py_EQ on the tuples, which does
Py_EQ on the elements.

This also accounts for the RichCompare calls which receive None: v may
be None, here, so t is (None, type(None)).

In IDLE, the situation is similar. com_add produces many compares with
types. In addition, sre.compile has

   type(s) in sre_compile.STRING_TYPES

which is the same test as the PyXML one. Finally, there is a
type-in-typetuple test inside Tkinter._cnfmerge.

Regards,
Martin