Boilerplate in rich comparison methods
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Sat Jan 13 20:24:14 EST 2007
On Sat, 13 Jan 2007 22:05:53 +0000, Neil Cerutti wrote:
>> Anyone want to argue that this is a worthwhile optimization? :)
>
> Perhaps. But first test it with "==".
Oh the ignominy! That's what happens when I run code at 6am :(
>>> x = CompareWithIdentity(1); y = CompareWithIdentity(1)
>>> timeit.Timer("x == y", "from __main__ import x,y").repeat()
[2.2971229553222656, 2.2821698188781738, 2.2767620086669922]
>>> timeit.Timer("x == x", "from __main__ import x,y").repeat()
[1.6935880184173584, 1.6783449649810791, 1.6613109111785889]
>>> x = Compare(1); y = Compare(1)
>>> timeit.Timer("x == y", "from __main__ import x,y").repeat()
[2.1717329025268555, 2.1361908912658691, 2.1338419914245605]
So for this simple case, testing for identity is a factor of 1.3 faster
when the objects are identical, and a factor of 1.1 slower if they aren't.
That suggests that if about 33% of your comparisons match by identity,
you'll break-even; any less than that, and the optimization is actually a
pessimation.
--
Steven.
More information about the Python-list
mailing list