473,795 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance penalty for using properties?

Now that I'm back to Python and all the new (to me) cool features,
I find I'm using properties a lot, i.e. I'm defining:

foo = property(fset=. .., fget=...)

for a number of properties, in many of my classes. I'm not using
them for anything performance critical yet, but could see myself
doing so in the future. Can anyone comment on the performance
costs associated with properties vs. simple attribute lookup?

Thanks,
Ken
Jul 18 '05 #1
5 3049
Kenneth McDonald wrote:
Now that I'm back to Python and all the new (to me) cool features,
I find I'm using properties a lot, i.e. I'm defining:

foo = property(fset=. .., fget=...)

for a number of properties, in many of my classes. I'm not using
them for anything performance critical yet, but could see myself
doing so in the future. Can anyone comment on the performance
costs associated with properties vs. simple attribute lookup?


It seems I'm becoming obsessed with timeit.py :-)

<property.py>
class Test(object):
def getvalue(self):
return self._value
value = property(getval ue)

t = Test()
t._value = 123
</property.py>

$ timeit.py -s"from property import t" "t._value"
1000000 loops, best of 3: 0.207 usec per loop
$ timeit.py -s"from property import t" "t.getvalue ()"
1000000 loops, best of 3: 0.918 usec per loop
$ timeit.py -s"from property import t" "t.value"
1000000 loops, best of 3: 1.03 usec per loop

Roughly factor five, most of the time being consumed by the implied function
call.

Peter
Jul 18 '05 #2
Kenneth McDonald wrote:
....
Can anyone comment on the performance
costs associated with properties vs. simple attribute lookup?

They can become a significant fraction of total run-time, and often wind
up showing as the heaviest methods in an entire application if they are
used pervasively (i.e. every attribute of every object is a property).
Even short runs of small systems so designed can give you 100s of 1000s
of calls to an individual method. OpenGLContext gets around this by
defining an accelerator for the __get__ method of the field properties.
At present BasicProperty doesn't have an equivalent accelerator (it has
far more "hooks" in the get methods), but I'll likely wind up coding one
eventually.

The real problem is the getters, by the way, by far the most common
situation is where you just want to alter the value or the name of the
dictionary key at which it is stored. The setters tend to be far less
frequently called in my experience, so you can leave them as python code
quite readily.

HTH,
Mike

_______________ _______________ _________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Jul 18 '05 #3
In article <ma************ *************** ***********@pyt hon.org>, Mike C. Fletcher wrote:
Kenneth McDonald wrote:
...
Can anyone comment on the performance
costs associated with properties vs. simple attribute lookup?

They can become a significant fraction of total run-time, and often wind
up showing as the heaviest methods in an entire application if they are
used pervasively (i.e. every attribute of every object is a property).
Even short runs of small systems so designed can give you 100s of 1000s
of calls to an individual method. OpenGLContext gets around this by
defining an accelerator for the __get__ method of the field properties.


What does "accelerato r" mean in this context? Obviously, it's something
that speeds up __get__ calls, but ihow is that accomplished?

If it's a simple wrapper, that means you can blissfully ignore property
costs until profiling shows it's a problem, which is a big win.

Joe
Jul 18 '05 #4
In article <sl************ ***********@g4. gateway.2wire.n et>,
Kenneth McDonald <km********@wis c.edu> wrote:

Now that I'm back to Python and all the new (to me) cool features,
I find I'm using properties a lot, i.e. I'm defining:

foo = property(fset=. .., fget=...)

for a number of properties, in many of my classes. I'm not using
them for anything performance critical yet, but could see myself
doing so in the future. Can anyone comment on the performance
costs associated with properties vs. simple attribute lookup?


That's sort-of not the point. Yes, plain attribute lookups are going to
be faster because they don't involve a function call. OTOH, new-style
classes already pay a penalty for properties because any simple
attribute lookup on an instance requires checking the whole MRO to make
sure there isn't a property. Finally, the proper comparison is against
getter/setter methods, which properties encapsulate nicely.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #5
Joe Mason wrote:
In article <ma************ *************** ***********@pyt hon.org>, Mike C. Fletcher wrote:

Kenneth McDonald wrote:
...

Can anyone comment on the performance
costs associated with properties vs. simple attribute lookup?
....
OpenGLConte xt gets around this by
defining an accelerator for the __get__ method of the field properties.
What does "accelerato r" mean in this context? Obviously, it's something
that speeds up __get__ calls, but ihow is that accomplished?

Accelerator in this context is a small C-coded Python extension with two
functions. One is a utility to retrieve a given key from an object's
__dict__. The other implements the __get__ operation (including lookup
of default method on the property on value lookup failure failure). All
told about 100 lines of C code.

http://cvs.sourceforge.net/viewcvs.p....c?view=markup

(and yes, I know accelerate is mis-spelled in the module names :) ).
If it's a simple wrapper, that means you can blissfully ignore property
costs until profiling shows it's a problem, which is a big win.

That's been my experience. I coded the entire VRML97 scenegraph engine
without the accelerator module and only added it when profiling showed
that the __get__ method was a hot-spot for performance. The
non-accelerated code is still there in case the C module isn't
available, of course. As I said, haven't gotten to the
accelerator-writing stage yet with BasicProperty, but I expect to soon.

Have fun,
Mike

_______________ _______________ _________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Jul 18 '05 #6

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

Similar topics

7
9251
by: Michael Andersson | last post by:
Hi! Does the use of exception handling induce a performance penalty during the execution of non exception handling code? Regards, /Michael
12
17309
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
4
3138
by: zzfreddybb | last post by:
We are using HP aCC compiler on a HP Itanium box ( 11.23) We are having some severe performance hits using exception handling ( try/catch ) scenarios. The online aCC documentation says: HP aC++ exception handling has no significant performance impact at compile-time or run-time. We have not found this to be the case at all.
1
2723
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived classes? f.i. ive got a class dbobject i project "Basesupport", compiles to Basesupport.dll. From dbobject i derive about 100 classes, thy all are located in Project
2
2272
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived classes? f.i. ive got a class dbobject i project "Basesupport", compiles to Basesupport.dll. From dbobject i derive about 100 classes, thy all are located in Project
1
1130
by: Steve Franks | last post by:
I have a web site built using ASP.NET 2.0 and Visual Studio 2005. If also have a C# assembly that was compiled using VS 2003, so this assembly runs under the 1.1 framework. If I use this assembly built for the 1.1 framework within my web app that uses the 2.0 framework, is there any performance penalty incurred here? In other words, am I creating some sort of "cross boundary" communication that has to occur between the 2.0 framework...
5
1991
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have memory error. Thus the derived classes has additional functionality, not additional data.
11
2659
by: Paul H | last post by:
Suppose I have a table called tblPeople and I want a field to illustrate whether each person prefers cats or dogs. I could do it one of three ways. 1. A plain text field Create a text field in tblPeople called PreferredPet. 2. A lookup field that stores text values. Create a text field in tblPeople called PreferredPetID and use it to lookup an identical text field in tblPreferredPets.
2
1762
by: william.david.anderson | last post by:
Hi there, I'm thinking of using a PREPARE statement inside a stored procedure, but am wondering about the performance penalty associated with calling PREPARE multiple times. Below is an example of the kind of statements I was thinking of using.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.