Created
May 8, 2012 11:35
-
-
Save reclosedev/2634384 to your computer and use it in GitHub Desktop.
Benchmark Python super() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import timeit | |
import sys | |
class A(object): | |
"""Original""" | |
def x(self): | |
return 2 | |
class B(A): | |
"""A.x(self)""" | |
def x(self): | |
return A.x(self) | |
class C(A): | |
"""super(self, B).x()""" | |
def x(self): | |
return super(C, self).x() | |
class D(A): | |
"""Py3k super().x()""" | |
def x(self): | |
return super().x() | |
NUMBER = 10000 | |
def bench_func(obj): | |
for i in range(1000): | |
obj.x() | |
objects = [A(), B(), C()] | |
if sys.version_info[0] == 3: | |
objects.append(D()) | |
for obj in objects: | |
print('%-20s %s' % (obj.__doc__, timeit.timeit(lambda : bench_func(obj), | |
number=NUMBER))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.7 | |
---------- | |
Original 1.58733854225 | |
A.x(self) 5.68280225777 | |
super(self, B).x() 6.59171414585 | |
Python 3.2 | |
---------- | |
Original 1.7320700459696752 | |
A.x(self) 3.103703411448495 | |
super(self, B).x() 6.925158743071903 | |
Py3k super().x() 5.659430906174519 | |
PyPy | |
---- | |
Original 0.0320502838277 | |
A.x(self) 0.252451937144 | |
super(self, B).x() 0.671578687892 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment