[pypy-issue] Issue #2422: Unexpected inversion of performance between objects and tuple (pypy/pypy)

Pierre-Antoine Champin issues-reply at bitbucket.org
Wed Oct 19 17:54:07 EDT 2016

New issue 2422: Unexpected inversion of performance between objects and tuple

Pierre-Antoine Champin:

Consider a simple object type made of a few values (e.g. geometrical point). It is possible to implement it with a class, or to use tuples. There are also intermediate solutions, such as a class with __slots__ or a namedtuple.
I was wondering what were the implications, in terms of performance, of those different options. My intuition was that, when it comes to comparison, tuples would be more efficient because their comparison operators are built-in, while for objects I needed to implement them with special methods.

So I wrote the attached test-case (also available [here](https://gist.github.com/pchampin/38e2a4ac66d6baea44f17a0ce3318293)).

It confirmed my intuition: with CPython (2 and 3), comparing tuples (including named tuples) is 10x faster than comparing objects with customized __eq__ and __lt__ methods.

However, with Pypy (5.1.1), the results are totally opposite: object comparison are 10 to 20 times faster than tuple comparison, which take roughly the same time in Pypy as in CPython!

This is all he more surprising that, in order to implement __eq__ and __lt__ for my classes, I just store their attributes in tuples and compare the two tuples... So why would this be slower when I directly compare tuples ??

It seems to me that Pypy could use the same optimization in both places, and that there might be, if not a bug, at least a missed opportunity here -- although my understanding of the inners of Pypy is admittedly limited..

More information about the pypy-issue mailing list