[Q] Overhead for lists as properties?

Oliver Hofmann setar at gmx.de
Fri Dec 7 15:34:17 CET 2001


'lo everyone!


Trying to figure out why appending to a list that is a class property
takes so much longer than appending to a class attribute; this seems
not to be the case for getting or setting other class attributes like
integers.

It seems setProperty is not called, btw:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       10   37.630    3.763   54.580    5.458 test.py:37(testSettingPropList)
  1000000   16.950    0.000   16.950    0.000 test.py:16(getB)
       10    3.910    0.391    3.910    0.391 test.py:32(testSettingNormList)
       10    1.500    0.150    1.500    0.150 test.py:27(testSettingProp)
       10    1.490    0.149    1.490    0.149 test.py:22(testSettingNorm)
        1    0.240    0.240   61.720   61.720 test.py:41(main)
        0    0.000             0.000          profile:0(profiler)
       20    0.000    0.000    0.000    0.000 test.py:4(__init__)
       20    0.000    0.000    0.000    0.000 test.py:9(__init__)
        1    0.000    0.000   61.720   61.720 profile:0(main())



Test code is appended below; I'm probably missing something obvious
here. 

Any help would be appreciated,


      Oliver

----------------------------
import profile, pstats

class Tester:
    def __init__(self):
        self.a = 0
        self.b = []

class Tester2:
    def __init__(self):
        self.__a = 0
        self.__b = []

    def getA(self): return self.__a
    def setA(self, new): self.__a = new

    def getB(self): return self.__b
    def setB(self, new): self.__b = new

    a = property(getA, setA)
    b = property(getB, setB)

def testSettingNorm():
    foo = Tester()
    for i in xrange(0, 100000):
       foo.a = i

def testSettingProp():
    foo = Tester2()
    for i in xrange(0, 100000):
        foo.a = i

def testSettingNormList():
    foo = Tester()
    for i in xrange(0, 100000):
        foo.b.append(i)

def testSettingPropList():
    foo = Tester2()
    for i in xrange(0, 100000):
        foo.b.append(i)

def main():
    for a in range(0, 10):
        testSettingNorm()
        testSettingProp()
        testSettingNormList()
        testSettingPropList()

if __name__ == '__main__':
    profile.run('main()', 'report')
    p = pstats.Stats('report')
    p.sort_stats('time').print_stats(10)



More information about the Python-list mailing list