Delayed initialization of Python performance improvement _python

Source: Internet
Author: User

The delay calculation of the class attribute is to define the attribute of the class as a property, only when it is accessed, and once accessed, the result will be cached and not calculated every time. The primary purpose of constructing a deferred computed property is to improve performance

Property

Before we cut to the chase, we understand the use of the property, which converts the access of the attribute to the invocation of the method.

Class Circle (object): 
 def __init__ (self, RADIUS): 
  Self.radius = Radius 
  
 @property
 def area (self): return 
  3.14 * Self.radius * * 2
  
C = Circle (4) 
print C.radius 
print C.area

As you can see, area is defined as the form of a method, but after adding @property, you can directly execute C.area as a property access.

Now the problem, each call C.area, will be calculated once, too waste CPU, how can only calculate once? This is lazy property.

Code implementation

Class Lazyproperty (object):
 def __init__ (Self, func):
  Self.func = func
 def __get__ (self, instance, owner):
  If instance is None: return
   self
  else:
   value = Self.func (instance)
   SetAttr (instance, self.func._ _name__, value) return
   value
import Math
class Circle (object):
 def __init__ (self, radius):
  Self.radius = Radius
 @LazyProperty
 def area (self):
  print ' Computing area ' return
  Math.PI * Self.radius * * 2
 @LazyProperty
 def perimeter (self):
  print ' Computing perimeter ' return
  2 * Math.PI * Self.radius

Description

An adorner class lazyproperty is defined for a delay calculation. Circle is the class that is used for testing, and the Circle class has three property radii (RADIUS), size (area), perimeter (perimeter). The area and perimeter properties are decorated by lazyproperty, and try the magic of Lazyproperty:

>>> C = Circle (2)
>>> print C.area
Computing area
12.5663706144
>>> Print C.area
12.5663706144

Computing area is printed once per calculation in area (), and "Computing area" is only printed once after two consecutive c.area calls. This benefits from Lazyproperty, which, once invoked once, will not be repeated regardless of the number of subsequent calls.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.