Unexpected inversion of performance between objects and tuple
Created originally on Bitbucket by pchampin (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).
It confirmed my intuition: with CPython (2 and 3), comparing tuples (including named tuples) is 10x faster than comparing objects with customized
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
__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..