Created
March 19, 2011 09:02
-
-
Save gnprice/877351 to your computer and use it in GitHub Desktop.
This file contains 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
# Times are PyPy / CPython. | |
# Python 2.7.0 (e837df3968a1, Mar 15 2011, 03:00:18) | |
# [PyPy 1.5.0-alpha0 with GCC 4.4.3] on linux2 | |
# Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) | |
# [GCC 4.4.5] on linux2 | |
# The CPython is 64-bit, the PyPy 32-bit, which I hope is not | |
# confounding anything. Both running on the same machine. | |
# Times measured with | |
# $ $PYTHON -m timeit -n10000 -r3 -s 'import bench_fnclass as b, gc; gc.enable()' "b.$func()" | |
# The gc.enable() is important, because timeit is dumb and disables | |
# GC, and in particular as_fn() takes an increasing and ridiculous | |
# amount of time when that is let stand. | |
# 0.439us / 6.66us (cool) | |
def as_fn2(): | |
def a(x): return x | |
def b(x): return a(x+'a') | |
def c(x): return b(x+'b') | |
def d(x): return c(x+'c') | |
for i in xrange(10): | |
d('d') | |
# 55us / 50us | |
def make_classes(): | |
class A(object): | |
def __init__(self, x): self.x = x | |
def f(self): return self.x | |
class B(object): | |
def __init__(self, x): self.x = x | |
def f(self): return A(self.x+'a').f() | |
class C(object): | |
def __init__(self, x): self.x = x | |
def f(self): return B(self.x+'b').f() | |
class D(object): | |
def __init__(self, x): self.x = x | |
def f(self): return C(self.x+'c').f() | |
# 195us / 80us (ouch) | |
def as_fn(): | |
class A(object): | |
def __init__(self, x): self.x = x | |
def f(self): return self.x | |
class B(object): | |
def __init__(self, x): self.x = x | |
def f(self): return A(self.x+'a').f() | |
class C(object): | |
def __init__(self, x): self.x = x | |
def f(self): return B(self.x+'b').f() | |
class D(object): | |
def __init__(self, x): self.x = x | |
def f(self): return C(self.x+'c').f() | |
for i in xrange(10): | |
D('d').f() | |
# 0.35us / 27us (yeah!) | |
class AsClass(object): | |
class A(object): | |
def __init__(self, x): self.x = x | |
def f(self): return self.x | |
class B(object): | |
def __init__(self, x): self.x = x | |
def f(self): return AsClass.A(self.x+'a').f() | |
class C(object): | |
def __init__(self, x): self.x = x | |
def f(self): return AsClass.B(self.x+'b').f() | |
class D(object): | |
def __init__(self, x): self.x = x | |
def f(self): return AsClass.C(self.x+'c').f() | |
@staticmethod | |
def go(): | |
for i in xrange(10): | |
AsClass.D('d').f() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment