Speed up properties?!

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Fri May 14 03:55:03 EDT 2004


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



More information about the Python-list mailing list