Are there any list comparison optimizations in Python?

Michael Hudson mwh at python.net
Tue Nov 13 05:59:10 EST 2001


David Bolen <db3l at fitlinxx.com> writes:

> Peter Hansen <peter at engcorp.com> writes:
> 
> > I just created two million-integer lists with "[0] * 1000000" and 
> > compared them, then reassigned the second name to reference the
> > first list and compared again.  There was a noticeable pause during
> > both compares (with "==").
> > 
> > Comparing two lists with different lengths *does* return
> > "immediately".
> > 
> > -empirically-yr's
> 
> On the non-empirical front, I peeked at the listobject.c source for
> its list_richcompare function and no, there doesn't appear to be any
> shortcut check for having both lists refer to the same object (at
> least in the latest CVS source).  It does do the length check (for ==
> and !=), but otherwise it's the full recursion with the generic
> comparison applied to each element.
> 
> It would be an easy optimization to add, although I'm wondering how
> much benefit typical real-life code would see.

I'm pretty sure that in Python <2.1 this "optimization" was enabled
for all comparisons (i.e. if "x is y", then "x == y" returns 1 without
calling any comparison methods).  It disappeared when rich comparisons
turned up; for one thing NaNs sohuld not compare equal to themselves
according to ieee754 (tho' they still seem to in 2.2).

Cheers,
M.

-- 
  It's a measure of how much I love Python that I moved to VA, where
  if things don't work out Guido will buy a plantation and put us to
  work harvesting peanuts instead.     -- Tim Peters, comp.lang.python



More information about the Python-list mailing list