M
Mirko Dziadzka
Hi all
I'm playing around with metaclasses and noticed, that there is small
but mesurable a performance difference in the code shown below. With a
more complex example I get a 5 percent performance penalty for using a
metaclass. Until today I assumed, that a metaclass has no performance
impact at all after class creation.
Can someone explain me the performance difference between the two classes
test1 and test2
Thank's in advance
Mirko
---- cut here -----
class meta(type):
pass
class test1(object):
__metaclass__ = meta # using type via meta here
def __init__(self):
self.foo = 42
def get_foo(self):
return self.foo
class test2(object):
__metaclass__ = type # using type directly
def __init__(self):
self.foo = 42
def get_foo(self):
return self.foo
# and here the performance test code ... it's only 2% on my machine
import time
for c in [test1, test2] * 10:
t = c()
start = time.time()
for i in xrange(10 * 1000 * 1000):
t.get_foo()
print c.__name__, time.time() - start
---- cut here -----
I'm playing around with metaclasses and noticed, that there is small
but mesurable a performance difference in the code shown below. With a
more complex example I get a 5 percent performance penalty for using a
metaclass. Until today I assumed, that a metaclass has no performance
impact at all after class creation.
Can someone explain me the performance difference between the two classes
test1 and test2
Thank's in advance
Mirko
---- cut here -----
class meta(type):
pass
class test1(object):
__metaclass__ = meta # using type via meta here
def __init__(self):
self.foo = 42
def get_foo(self):
return self.foo
class test2(object):
__metaclass__ = type # using type directly
def __init__(self):
self.foo = 42
def get_foo(self):
return self.foo
# and here the performance test code ... it's only 2% on my machine
import time
for c in [test1, test2] * 10:
t = c()
start = time.time()
for i in xrange(10 * 1000 * 1000):
t.get_foo()
print c.__name__, time.time() - start
---- cut here -----