Detailed explanation of using Python delayed initialization to improve performance

Source: Internet
Author: User
The so-called latency calculation of class attributes defines the class attributes as a property, which is calculated only when Accessed. once accessed, the results will be cached, you do not need to calculate the delay calculation of the so-called class attribute every time. it defines the class attribute as a property and is only calculated during access. once accessed, the results will be cached and do not need to be calculated every time. The main purpose of constructing a delayed computing attribute is to improve performance.

Property

Before proceeding to the topic, let's take a look at the usage of property. property can convert access to properties into method calls.

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, although area is defined as a method, after adding @ property, you can directly execute c. area as a property access.

Now the question is, every call to c. area will be calculated once, which is a waste of cpu. how can we calculate only 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 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

Defines a LazyProperty class for delayed computing. Circle is used for testing. The Circle class has three attributes: radius, area, and perimeter ). LazyProperty is decorated with the property of area and perimeter. let's try the magic of LazyProperty:

>>> c = Circle(2)>>> print c.areaComputing area12.5663706144>>> print c.area12.5663706144

"Computing area" is printed every time calculated in area (). after calling c. area twice in a row, "Computing area" is printed only once. This benefits from LazyProperty. after a call, no matter how many subsequent calls are called, the calculation will not be repeated.

The above is the detailed description of the instance for improving performance by using Python delay initialization. For more details, please refer to other articles in the first PHP community!

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.