Are there any list comparison optimizations in Python?

David Bolen db3l at fitlinxx.com
Mon Nov 12 23:45:51 EST 2001


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.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list