Comparison of PHP Static Delay Binding and Normal Static Efficiency

  • 2021-08-10 07:21:13
  • OfStack

Comparison of PHP Static Delay Binding and Normal Static Efficiency

It's just a simple experiment, which compares the efficiency of lower delay binding with that of non-delay binding

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

Lab 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; 
  } 
} 

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

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

The process is not much said, and the environment is PHP 5.4. 27. The test results are directly on:

There are two situations,

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

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

It takes about 0.3 seconds between 2.8 s and 3.2 s, so it should be negligible

Added: After adding some test methods, if C class inherits A class and overloads some static member variables in A class, the more overloads, the closer the speed is to B class (non-delay), but the speed of A class will still be slower than B class and C class

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: