Detailed explanation of the comparison between static delayed binding and ordinary static efficiency in PHP

黄舟
Release: 2023-03-16 18:02:02
Original
1398 people have browsed it

Comparison of PHP static delayed binding and ordinary static efficiency

It’s just a simple experiment, comparing the efficiency of delayed binding and non-delayed

Delayed binding mainly uses the static keyword to replace the original self, but the function is very powerful

Experimental code:


class A { 
  protected static $cc1 = array('a1', 'b', 'c', 'd'); 
  protected static $cc2 = array('a2', 'b', 'c', 'd'); 
  protected static $cc3 = array('a3', 'b', 'c', 'd'); 
  protected static $cc4 = array('a4', 'b', 'c', 'd'); 
  protected static $cc5 = array('a5', 'b', 'c', 'd'); 
 
  public static function n1() { 
    return static::$cc1; 
  } 
  public static function n2() { 
    return static::$cc2; 
  } 
  public static function n3() { 
    return static::$cc3; 
  } 
  public static function n4() { 
    return static::$cc4; 
  } 
  public static function n5() { 
    return static::$cc5; 
  } 
} 
 
class C extends A { 
 
} 
 
class B { 
  protected static $cc1 = array('a1', 'b', 'c', 'd'); 
  protected static $cc2 = array('a2', 'b', 'c', 'd'); 
  protected static $cc3 = array('a3', 'b', 'c', 'd'); 
  protected static $cc4 = array('a4', 'b', 'c', 'd'); 
  protected static $cc5 = array('a5', 'b', 'c', 'd'); 
 
  public static function n1() { 
    return self::$cc1; 
  } 
  public static function n2() { 
    return self::$cc2; 
  } 
  public static function n3() { 
    return self::$cc3; 
  } 
  public static function n4() { 
    return self::$cc4; 
  } 
  public static function n5() { 
    return self::$cc5; 
  } 
}
Copy after login

There are three classes A, B, and C above, all of which are static member variables and methods. Among them,

A class uses static delay,
B class is non-delay,
Class C inherits class A and implements delayed binding of static member variables and methods.

I won’t go into details about the process. The environment is PHP5.4.27. Directly upload the test results:

There are two situations,

1. When there are only A and B class (that is, no class is allowed to inherit class A), there is almost no difference in efficiency

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

Loops 100,000 times, and the time difference between 2.8s ~ 3.2s is about 0.3 seconds. It should be negligible

Addition:Later, some test methods were added. If class C inherits class A and overloads some of the static member variables in class A, reload The more loads, the closer the speed will be to Class B (non-delayed), but the speed of Class A will still be slower than Class B and Class C

The above is the detailed content of Detailed explanation of the comparison between static delayed binding and ordinary static efficiency in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template