473,394 Members | 1,658 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,394 software developers and data experts.

Speed up properties?!

Hi,

I have a class with some properties. I would like to verify that only
valid values are assigned to the properties using assert. Therefore I
code setters and getters and use property() to convert these to have a
real property.

Since the verification is only performed in __debug__ runs the
property() is quite a lot of overhead. I tried to circumvent it. This
is my result so far:
class C(object):
def __init__(self):
self._x = 5
if not __debug__:
self.x = property(self._x, self._x)

def getX(self):
return self._x

def setX(self, v):
assert 0 <= v <= 5
self._x = v

if __debug__:
x = property(getX, setX)

o = C()
def test():
o.x

if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()

As you can see, in non __debug__ runs the accesses of the x property do
not result in calls to getX or setX. There I can get a speedup of 2!
But to be honest: I don't like this aproach. So is there some better,
cleaner way?

Peer


Jul 18 '05 #1
2 1589
Dr. Peer Griebel wrote:
I have a class with some properties. I would like to verify that only
valid values are assigned to the properties using assert. Therefore I
code setters and getters and use property() to convert these to have a
real property.

Since the verification is only performed in __debug__ runs the
property() is quite a lot of overhead. I tried to circumvent it. This
is my result so far:
class C(object):
def __init__(self):
self._x = 5
if not __debug__:
self.x = property(self._x, self._x)

def getX(self):
return self._x

def setX(self, v):
assert 0 <= v <= 5
self._x = v

if __debug__:
x = property(getX, setX)

o = C()
def test():
o.x

if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()

As you can see, in non __debug__ runs the accesses of the x property do
not result in calls to getX or setX. There I can get a speedup of 2!
But to be honest: I don't like this aproach. So is there some better,
cleaner way?


class C(object):
def __init__(self):
self.setX(5)

def setX(self, x):
if not 0 <= x <= 5:
raise ValueError
self.x = x

# for speed comparison only:
y = property(lambda s: s.x)

o = C()

With the above, my timings show an even greater speed difference:

$ timeit.py -s'from propspeed import o' 'o.x'
1000000 loops, best of 3: 0.235 usec per loop
$ timeit.py -s'from propspeed import o' 'o.y'
1000000 loops, best of 3: 1.04 usec per loop

Special debug code is dangerous because it can hide errors in the final
version. So, if speed is that important and read access paramount, go for
the asymmetric solution and use o.x for read access and o.setX() - always
checked - for write access.
However, my guess is that in most cases you will hardly feel any impact on
the overall speed of your program, and in that case I'd recommend an x
property - again with the value check preserved in the non-debug version.
If you then encounter a performance problem, you probably can pin it down to
some inner loop, and replacing o.x with o._x (only) there should be
painless.

Peter
Jul 18 '05 #2
On Fri, 14 May 2004 08:53:30 +0200, in comp.lang.python Peter Otten
wrote:

[comparing direct attribute access and indirect through property get]

A slight addition to your code for another alternative (2.4 only) of
property gets (the z property):

class C(object):
def __init__(self):
self.setX(5)

def setX(self, x):
if not 0 <= x <= 5:
raise ValueError
self.x = x

# for speed comparison only:
y = property(lambda s: s.x)

from operator import attrgetter
z = property(attrgetter("x"))

o = C()

yields (on my laptop):

C:\Temp>timeit -s "from propspeed import o" o.x
1000000 loops, best of 3: 0.655 usec per loop

C:\Temp>timeit -s "from propspeed import o" o.y
100000 loops, best of 3: 2.36 usec per loop

C:\Temp>timeit -s "from propspeed import o" o.z
1000000 loops, best of 3: 1.51 usec per loop
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #3

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

Similar topics

0
by: Michael Bourgon | last post by:
Here's something I found a few months ago, and it seems to come in handy pretty often, so I figured I'd share. Disclaimer: you are modifying a SYSTEM TABLE. This may not work in the next...
5
by: Christopher Boyce | last post by:
I am using the fw_menu.js from John Ahlquist (October 2000). The menu works great, but the problem I am having is with it's speed in IE6. The problem is I am making a menu for each item in a...
4
by: trint | last post by:
Ok, This script is something I wrote for bringing up a report in reporting services and it is really slow...Is their any problems with it or is their better syntax to speed it up and still provide...
11
by: WindAndWaves | last post by:
Hi everyone, I hope I will not get banned for asking toooo many questions. I am just really excited to have this platform ;-) I have the following function: Function IsProperty(pptname) as...
6
by: Chua Wen Ching | last post by:
Hi there, 1) I am looking for the best collections techniques to be used in my program. Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???...
4
by: cameron | last post by:
I have always been under the impression that LDAP was optimized for speed. Fast queries, fast access, slower writes. I have a block of data in LDAP and in SQL. Exact same data. The query is fast...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
3
by: Chris288 | last post by:
Hi, I have a problem where our app when compiled in VS2005 runs about 50% the speed it attains in VS2003. This is an unmanaged C++ app. I have tried most combinations of the optimization and...
11
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.