Performance penalty for using properties?

Peter Otten __peter__ at web.de
Sat Mar 13 09:47:56 CET 2004


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(getvalue)

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



More information about the Python-list mailing list