Python @property versus method performance - which one to use?

后端 未结 6 1498
孤独总比滥情好
孤独总比滥情好 2021-01-04 03:31

I have written some code that uses attributes of an object:

class Foo:
    def __init__(self):
        self.bar = \"baz\"
myFoo = Foo()
print (myFoo.bar)
         


        
6条回答
  •  太阳男子
    2021-01-04 04:12

    Wondering about performance is needless when it's so easy to measure it:

    $ python -mtimeit -s'class X(object):
    >   @property
    >   def y(self): return 23
    > x=X()' 'x.y'
    1000000 loops, best of 3: 0.685 usec per loop
    $ python -mtimeit -s'class X(object):
    
      def y(self): return 23
    x=X()' 'x.y()'
    1000000 loops, best of 3: 0.447 usec per loop
    $ 
    

    (on my slow laptop -- if you wonder why the 2nd case doesn't have secondary shell prompts, it's because I built it from the first with an up-arrow in bash, and that repeats the linebreak structure but not the prompts!-).

    So unless you're in a case where you know 200+ nanoseconds or so will matter (a tight inner loop you're trying to optimize to the utmost), you can afford to use the property approach; if you do some computations to get the value, the 200+ nanoseconds will of course become a smaller fraction of the total time taken.

    I do agree with other answers that if the computations become too heavy, or you may ever want parameters, etc, a method is preferable -- similarly, I'll add, if you ever need to stash the callable somewhere but only call it later, and other fancy functional programming tricks; but I wanted to make the performance point quantitatively, since timeit makes such measurements so easy!-)

提交回复
热议问题