Detailed analysis of PHP magic function performance code

Source: Internet
Author: User
I once remember Laruence mentioned that it was not recommended to use the "magic method". after that, I would subconsciously think about how to use the magic method. Is this a good photo? Since I have been busy with work and learning for the past two years...

I once remember Laruence mentioned that it was not recommended to use the "magic method". after that, I would subconsciously think about how to use the magic method. Is this a good photo? Since I have been busy with work and learning new knowledge for the past two years, I have not made any in-depth exploration on this hurdle. This year is my year of in-depth study, therefore, we must fix this issue now. Let's take a look at what Laruence's blog once mentioned:

When I share the PPT to my colleagues in the company, some people will question whether the magic methods are not used?

The optimization suggestion is to prevent abuse and use it without authorization. if you are aware of what is slow and what is fast when writing code to avoid unnecessary calls to the magic method, it is the result pursued by this optimization suggestion.

Doubt
  1. Is the magic method really inferior in performance?

  2. Is there a problem with the performance of using magic methods in PHP 7?

  3. How can we reasonably use magic methods?

Solution

In the face of my doubts, my solution is:

  • Time difference between the use of magic methods and the execution of scripts without magic methods

  • Execute the script n times in a PHP5.6.26-1

  • Average/minimum/maximum value of statistical execution time

  • Execute the script n times in a PHP7.0.12-2

  • Average/minimum/maximum value of statistical execution time

Currently, my personal abilities are limited. you can only use this method. if you have better solutions or suggestions, please let me know. thank you, haha ~

Test _ construct

First, let's take a look at the construct experiment. the php script is as follows:

 
 
 
  • PHP5.6 does not use the following Magic data method, in microseconds μm

// Script 10000 consecutive calls in PHP5.6 sh test 10000 no_magic php5 construct // run the data statistics script sh analysis. /logs/_ construct_no_magic_php5.log 10000 // result avg: 34 μmmax: 483 μmmin: 26 μm
  • PHP5.6 uses the following Magic data, in microsecond μm

// Script 10000 consecutive calls in PHP5.6 sh test 10000 magic php5 construct // run the data statistics script sh analysis. /logs/_ construct_magic_php5.log 10000 // result avg: 28 μm Max: 896 μm: 20 μm
  • PHP7.0 does not use the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 no_magic php construct // run the data statistics script sh analysis. /logs/_ construct_no_magic_php.log 10000 // result avg: 19 μm Max: 819 μm: 13 μm
  • PHP7.0 uses the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 magic php construct // run the data statistics script sh analysis. /logs/_ construct_magic_php.log 10000 // result avg: 14 μm Max: 157 μm: 10 μm

The above data shows that:

The average execution time of scripts using _ construct as the constructor is faster than that using the class name as the constructor,About 5 to 6 microsecondsWhether in php5.6 or php7.0.

_ Call

Next, let's take a look at the _ call experiment. the php script is as follows:

  Test () ;}else {$ instance = new ClassTwo (); $ instance-> test () ;}$ B = getmicrotime (); echo ($ B-$ ). "\ n ";
  • PHP5.6 does not use the following Magic data method, in microseconds μm

// Call the script 10000 times consecutively in PHP5.6 sh test 10000 no_magic php5 call // run the data statistics script sh analysis. /logs/_ call_no_magic_php5.log 10000 // result avg: 27 μ mmax: 206 μmmin: 20 μm
  • PHP5.6 uses the following Magic data, in microsecond μm

// Script 10000 consecutive calls in PHP5.6 sh test 10000 magic php5 call // run the data statistics script sh analysis. /logs/_ call_magic_php5.log 10000 // result avg: 29 μm Max: 392 μm: 22 μm
  • PHP7.0 does not use the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 no_magic php call // run the data statistics script sh analysis. /logs/_ call_no_magic_php.log 10000 // result avg: 16 μm Max: 256 μm: 10 μm
  • PHP7.0 uses the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 magic php call // run the data statistics script sh analysis. /logs/_ call_magic_php.log 10000 // result avg: 18 μmmax: 2459 μmmin: 11 μm

The above data shows that:

The average execution time of the _ call script is slower than that of the script,About 2 microseconds slowWhether in php5.6 or php7.0.

_ CallStatic

Next, let's take a look at the _ callStatic experiment. the php script is as follows:

  
  
  
  • PHP5.6 does not use the following Magic data method, in microseconds μm

// In PHP5.6, the script is called for 10000 consecutive times. sh test 10000 no_magic php5 callStatic // run the data statistics script sh analysis. /logs/_ callStatic_no_magic_php5.log 10000 // result avg: 25 μm Max: 129 μm: 19 μm
  • PHP5.6 uses the following Magic data, in microsecond μm

// Call the script 10000 consecutive times in PHP5.6 sh test 10000 magic php5 callStatic // run the data statistics script sh analysis. /logs/_ callStatic_magic_php5.log 10000 // result avg: 28 μm Max: 580 μm: 20 μm
  • PHP7.0 does not use the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 no_magic php callStatic // run the data statistics script sh analysis. /logs/_ callStatic_no_magic_php.log 10000 // result avg: 14 μm Max: 130 μm: 9 μm
  • PHP7.0 uses the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 magic php callStatic // run the data statistics script sh analysis. /logs/_ callStatic_magic_php.log 10000 // result avg: 14 μm Max: 159 μm: 10 μm

The above data shows that:

The average execution time of the _ callStatic script in php5.6 is slower,About 3 microseconds slowIn php7.0, the average execution time of scripts with _ callStatic is roughly equal to that without _ callStatic;

_ Set

Next, let's take a look at the _ set experiment. the php script is as follows:

   SomeVariable = $ value ;}} /*** use _ set to set private attributes */class ClassTwo {/*** private attributes ** @ var string */private $ someVariable = 'private '; public function _ construct () {# code ...} public function _ set ($ name = '', $ value ='') {$ this-> $ name = $ value ;}}$ a = getmicrotime (); if ($ is_use_magic = 'no _ magic ') {$ instance = new ClassOne (); $ instance-> setSomeVariable ('public ');} else {$ instance = new ClassTwo (); $ instance-> someVariable = 'public';} $ B = getmicrotime (); echo ($ B-$ ). "\ n ";
  • PHP5.6 does not use the following Magic data method, in microseconds μm

// Script 10000 consecutive calls in PHP5.6 sh test 10000 no_magic php5 set // run the data statistics script sh analysis. /logs/_ set_no_magic_php5.log 10000 // result avg: 31 μmmax: 110 μmmin: 24 μm
  • PHP5.6 uses the following Magic data, in microsecond μm

// Call the script 10000 consecutive times in PHP5.6 sh test 10000 magic php5 set // run the data statistics script sh analysis. /logs/_ set_magic_php5.log 10000 // result avg: 33 μm Max: 138 μm: 25 μm
  • PHP7.0 does not use the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 no_magic php set // run the data statistics script sh analysis. /logs/_ set_no_magic_php.log 10000 // result avg: 15 μm Max: 441 μm: 11 μm
  • PHP7.0 uses the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 magic php set // run the data statistics script sh analysis. /logs/_ set_magic_php.log 10000 // result avg: 17 μm Max: 120 μm: 11 μm

The above data shows that:

The average execution time of scripts using _ set is slower than that of none,About 2 microseconds slowWhether in php5.6 or php7.0.

_ Get

Next, let's take a look at the _ get Experiment. the php script is as follows:

   SomeVariable ;}}/*** use _ get to obtain private attributes */class ClassTwo {/*** private attributes ** @ var string */private $ someVariable = 'private '; public function _ construct () {# code ...} public function _ get ($ name = '') {return $ this-> $ name ;}}$ a = getmicrotime (); if ($ is_use_magic = 'no _ magic ') {$ instance = new ClassOne (); $ instance-> getSomeVariable ();} else {$ instance = new ClassTwo (); $ instance-> someVariable;} $ B = getmicrotime (); echo ($ B-$ ). "\ n ";
  • PHP5.6 does not use the following Magic data method, in microseconds μm

// Call the script 10000 times consecutively in PHP5.6 sh test 10000 no_magic php5 get // run the data statistics script sh analysis. /logs/_ get_no_magic_php5.log 10000 // result avg: 28 μmmax: 590 μmmin: 20 μm
  • PHP5.6 uses the following Magic data, in microsecond μm

// Call the script 10000 consecutive times in PHP5.6 sh test 10000 magic php5 get // run the data statistics script sh analysis. /logs/_ get_magic_php5.log 10000 // result avg: 28 μm Max: 211 μm: 22 μm
  • PHP7.0 does not use the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 no_magic php get // run the data statistics script sh analysis. /logs/_ get_no_magic_php.log 10000 // result avg: 16 μm Max: 295 μm: 10 μm
  • PHP7.0 uses the magic data method as follows, in microseconds μm

// Call the script 10000 consecutive times in PHP7.0 sh test 10000 magic php get // run the data statistics script sh analysis. /logs/_ get_magic_php.log 10000 // result avg: 19 μm Max: 525 μm: 12 μm

The above data shows that:

In php5.6, the average execution time of scripts using _ get is roughly equal to that without _ get; in php7.0, the average execution time of scripts using _ get is slower than not using,About 3 microseconds slow.

Conclusion

Here we mainly test _ construct (), _ call (), _ callStatic (), _ get (), _ set () these five commonly used magic functions can be replaced by other implementations. After passing the test above, I will come back to answer my questions.

  1. Is the magic method really inferior in performance?

A: Apart from using _ construct, it takes less than 10 minutes to use other magic methods.

  1. Is there a problem with the performance of using magic methods in PHP 7?

A: The difference between using and not using magic methods in PHP7 is almost the same as that in PHP5.6.

  1. How can we reasonably use magic methods?

A: Through the test, we can see that the execution time difference between not using magic methods is roughly within 10 subtle points, therefore, if the magic method can effectively save our development costs and optimize our code structure, we should be able to consider sacrificing less than 10 nuances. And _ construct is faster, so there should be no objection to using _ construct.

The above is a detailed analysis of PHP magic function performance code. For more information, see other related 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.