PHP static delay binding and general static efficiency simple comparison

Source: Internet
Author: User

It's just a simple little experiment that contrasts the latency-bound and non-delayed efficiencies

Lazy binding is mostly the use of the static keyword to replace the original self, but the function is very powerful


Experiment Code:

Class A {protected static $CC 1 = Array (' A1 ', ' B ', ' C ', ' d ');p rotected static $CC 2 = Array (' A2 ', ' B ', ' C ', ' d ');p rotected s Tatic $CC 3 = Array (' A3 ', ' B ', ' C ', ' d ');p rotected static $CC 4 = array (' A4 ', ' B ', ' C ', ' d ');p rotected static $CC 5 = Array (' A5 ', ' B ', ' C ', ' d ');p ublic static function n1 () {return static:: $CC 1;} public static function N2 () {return static:: $CC 2;} public static function N3 () {return static:: $CC 3;} public static function N4 () {return static:: $CC 4;} public static function N5 () {return static:: $CC 5;}} Class C extends A {}class B {protected static $CC 1 = Array (' A1 ', ' B ', ' C ', ' d ');p rotected static $CC 2 = Array (' A2 ', ' B ', ' C ', ' d ');p rotected static $CC 3 = Array (' A3 ', ' B ', ' C ', ' d ');p rotected static $CC 4 = array (' A4 ', ' B ', ' C ', ' d ');p rotected s Tatic $CC 5 = array (' A5 ', ' B ', ' C ', ' d ');p ublic static function n1 () {return self:: $CC 1;} public static function N2 () {Return to self:: $CC 2;} public static function N3 () {Return to self:: $CC 3;} public static function N4 () {Return to self:: $CC 4;} Public Static function N5 () {return self:: $CC 5;}} 


A total of a, B, C three classes, which are all static member variables and methods, where

The Class A uses a static delay,

Class B is non-delayed,

Class C is a deferred binding that inherits the Class A to implement static member variables and methods.


The process is not much to say, the environment is PHP5.4.27 directly on the test results:


There are two situations,

1. When there is only a, B class (That is, no class inherits from Class A), there is almost no difference in efficiency

2. When Class A is inherited by Class C, the class A performance using static delay binding will be slightly worse than Class B (as long as Class A has inherited classes will become slower)


Cycle 100,000 times, time-consuming between 2.8s ~ 3.2s gap in 0.3 seconds or so, it should still be negligible


Add a:

And then I added some test methods.

If the class C inherits the Class A, the static member variables in the Class A are overloaded, the more overloads are the faster and the class B (non-delay) is closer, but the class A is still slower than Class B and class C




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.