Performance penalty for using properties?
Robert Brewer
fumanchu at amor.org
Sat Mar 13 12:57:45 EST 2004
Peter Otten wrote:
> 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 :-)
ISTR Alex Martelli went through such a phase.. ;)
> <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.
I've always used "property" as sugar for when I would otherwise have
overriden __getattr(ibute)__, in which case the performance is not that
different.
FuManChu
More information about the Python-list
mailing list