473,378 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

metaclasses and performance

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 -----
Jun 19 '07 #1
2 1636
Mirko Dziadzka wrote:
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 don't know if C asserts are active in release Python, but for
new-style classes one thing that happens during attribute lookup is that
an object's class is asserted to be an instance of type. Class test2
immediately passes the test since its type is "type". But test1's type
is meta, so a function is called to check that meta is a subclass of
"type". This may be the cause of the slowdown. Otherwise attribute
lookups for test1 and test2 instances are identical.
--
Lenard Lindstrom
<le***@telus.net>
Jun 19 '07 #2
Lenard Lindstrom <le***@telus.net wrote:
I don't know if C asserts are active in release Python, but for
new-style classes one thing that happens during attribute lookup is that
an object's class is asserted to be an instance of type.
Thank's for the explanation. My Linux distribution does not set NDEBUG
when compiling the python package so asserts are still in place. A self
compiled python does not use the asserts and is 20% faster in my micro
benchmark.

Mirko

Jun 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
3
by: Mike C. Fletcher | last post by:
Slides from my PyGTA presentation on Tuesday, focusing mostly on why/where you would want to use meta-classes, are available in PDF format: ...
2
by: Santiago Aguiar | last post by:
Im trying to use AOP techniques on python but, even if I understand the idea behind metaclasses, I cant find a way to use AOP in a dynamic way. What im trying to do is to weave an aspect to a...
27
by: Michele Simionato | last post by:
> Hello, my name is Skip and I am metaclass-unaware. I've been programming in > Python for about ten years and I have yet to write a metaclass. At first I > thought it was just that metaclasses...
4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
13
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out...
1
by: Hartmut Goebel | last post by:
Hi, I'm currently implementing a tool for auto-generating webforms from a desription file. The output should become a php-script, an asp-script, zope-formulator or such. Each if it may (or may...
0
by: Jan-Ole Esleben | last post by:
Hi! I've just posted a question about metaclasses in ZOPE on the ZOPE list, and one of the replies said that metaclasses (at least "painless" metaclasses) cannot be used without new-style...
1
by: Alan Isaac | last post by:
Forman's book is out of print. Is there a good substitute? Thanks, Alan Isaac
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.