Delayed initialization of Python performance improvements

Source: Internet
Author: User

The so-called deferred computation of a class property is defined as a property of the class, which is evaluated only at the time of access, and once accessed, the result is cached and not calculated every time. The main purpose of constructing a deferred computed property is to improve performance

Property

Before we get to the point, we understand the use of the property, which transforms the access of the attribute into a call to 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 a form of a method, but with @property, you can execute C.area directly as a property access.

Now the problem, every call C.area, will be calculated once, too wasted CPU, how to calculate only once? This is the Lazy property.

Code implementation

Class Lazyproperty (object): Def __init__ (Self, func):  Self.func = func def __get__ (self, instance, owner):  If INS Tance is None:   return self  else:   value = Self.func (instance)   SetAttr (instance, self.func.__name__, Value)   return valueimport MathClass 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 that defines a deferred calculation. Circle is the class used for testing, and the Circle class has three attribute radius (RADIUS), area, perimeter (perimeter). The area and perimeter properties are lazyproperty decorated, below to try Lazyproperty's Magic:

>>> C = Circle (2) >>> print c.areacomputing area12.5663706144>>> print c.area12.5663706144


The "Computing area" is printed once per calculation in area (), and the "Computing area" is only printed once after a continuous call of two c.area. This is thanks to Lazyproperty, which, once invoked, does not repeat calculations regardless of the number of subsequent calls.

Related Article

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.