Once remember that bird brother Laruence not recommended the use of "magic Method", since then once involved in the use of magic method, I will subconsciously think about it, so good photo? Since this one to two years has been busy working and learning new knowledge, so in this snag has not done in-depth exploration has been stupefied, this year is my deep study of the year, so now must be a close to the problem. Let's take a look at the bird Brother Laruence blog once mentioned:
When I share the PPT to the company's colleagues, there will be questions, the Magic method is not allowed to use?
The proposal to optimize is to prevent abuse and unbridled use of the people. If you can write the code, you can realize, what is slow, what is fast, so as to avoid some unnecessary calls to the Magic method, that is the effect of the optimization proposal to pursue
Doubts
Does the Magic method really have poor performance?
Is there a problem with the performance of the Magic method in PHP7?
How should we reasonably use the Magic method?
Scheme
In the face of my doubts, my plan is:
Statistical comparison of time differences using magic methods and script execution without using magic methods
Sequential execution of script n times under Php5.6.26-1
Average/minimum/maximum of statistical execution time
Sequential execution of script n times under Php7.0.12-2
Average/minimum/maximum of statistical execution time
At present, personal ability is limited, only in this way, if you have a better plan or advice can tell me, thank you, haha~
Test
Construct
First, let's take a look at the experiment of the constructor construct, the PHP script is as follows:
<?php/** * Magic Method Performance Exploration * * constructor * * * @author tigerb
PHP5.6 in a sequential call script 10,000 times SH test 10000 no_magic php5 construct//Run Data statistics script SH analysis./logs/construct_no_magic_php5.log 10000//Results avg:34μmmax:483μmmin:26μm
PHP5.6 sequential call script 10,000 times SH test 10000 magic PHP5 construct//Run data statistics script SH analysis./logs/construct_magic_php5.log 10000//Results avg:28μmmax:896μmmin:20μm
PHP7.0 in a sequential call script 10,000 times SH test 10000 no_magic PHP construct//run data statistics script SH analysis./logs/construct_no_magic_php.log 10000 Result avg:19μmmax:819μmmin:13μm
PHP7.0 sequential call script 10,000 times sh test 10000 magic PHP construct//run data statistics script SH analysis./logs/construct_magic_php.log 10000//Results av g:14μmmax:157μmmin:10μm
From the above data we can see:
The average time to execute scripts that use construct as constructors is faster than using class masterpieces as constructors, about 5 to 6 microseconds , whether in php5.6 or php7.0.
Pager
Next, let's take a look at the call experiment, with the following PHP script:
<?php/** * Magic Method Performance Exploration * * constructor * * * @author tigerb
PHP5.6 consecutive call script 10,000 times SH test 10000 no_magic php5 call//Run Data statistics script SH analysis./logs/call_no_magic_php5.log 10000//result avg: 27μmmax:206μmmin:20μm
PHP5.6 call script 10,000 times SH test 10000 magic PHP5 call//Run data statistics script SH analysis./logs/call_magic_php5.log 10000//Results Avg:29μmma x:392μmmin:22μm
PHP7.0 call script 10,000 times SH test 10000 no_magic PHP call//run data statistics script SH analysis./logs/call_no_magic_php.log 10000//Results avg:16 μmmax:256μmmin:10μm
PHP7.0 call script 10,000 times sh test 10000 magic PHP call//run data statistics script SH analysis./logs/call_magic_php.log 10000//Results Avg:18μmmax: 2459μmmin:11μm
From the above data we can see:
The average time it takes to execute a script using call is slower than not being used, about 2 microseconds , either in php5.6 or php7.0.
Callstatic
Next, let's take a look at Callstatic's experiment with the following PHP script:
<?php/** * Magic Method Performance Exploration * * Static overloaded functions * * @author Tigerb
PHP5.6 in a sequential call script 10,000 times SH test 10000 no_magic php5 callstatic//Run Data statistics script SH analysis./logs/callstatic_no_magic_php5.log 10000//Results avg:25μmmax:129μmmin:19μm
PHP5.6 in sequential call script 10,000 times SH test 10000 magic PHP5 callstatic//Run data statistics script SH analysis./logs/callstatic_magic_php5.log 10000// Result avg:28μmmax:580μmmin:20μm
PHP7.0 in a sequential call script 10,000 times SH test 10000 no_magic PHP callstatic//run data statistics script SH analysis./logs/callstatic_no_magic_php.log 10000//Results avg:14μmmax:130μmmin:9μm
PHP7.0 consecutive call script 10,000 times sh test 10000 magic PHP callstatic//run data statistics script SH analysis./logs/callstatic_magic_php.log 10000//Results avg:14μmmax:159μmmin:10μm
From the above data we can see:
The average time to execute a script that uses callstatic in php5.6 is slower than not used, about 3 microseconds in length, and the average time to execute a script using callstatic in php7.0 is roughly equal to not using Callstatic;
Set
Next, let's take a look at set experiments, the PHP script is as follows:
<?php/** * Magic Method Performance Exploration * * Set private Property Set * * * @author tigerb
PHP5.6 call script 10,000 times SH test 10000 no_magic php5 set//Run Data statistics script SH analysis./logs/set_no_magic_php5.log 10000//Results avg:31 μmmax:110μmmin:24μm
PHP5.6 in sequential call script 10,000 times SH test 10000 magic PHP5 set//Run data statistics script SH analysis./logs/set_magic_php5.log 10000//Results Avg:33μmmax: 138μmmin:25μm
PHP7.0 call script 10,000 times SH test 10000 no_magic PHP set//run data statistics script SH analysis./logs/set_no_magic_php.log 10000//Results avg:15μm max:441μmmin:11μm
PHP7.0 sequential call script 10,000 times sh test 10000 magic PHP set//run data statistics script SH analysis./logs/set_magic_php.log 10000//Results Avg:17μmmax:1 20μmmin:11μm
From the above data we can see:
The average time to execute a script using set is slower than not used, about 2 microseconds , either in php5.6 or php7.0.
Get
Next, let's take a look at the get experiment, with the following PHP script:
<?php/** * Magic Method Performance Exploration * * Read private Property Get * * @author tigerb
PHP5.6 call script 10,000 times SH test 10000 no_magic php5 get//Run Data statistics script SH analysis./logs/get_no_magic_php5.log 10000//Results avg:28 μmmax:590μmmin:20μm
PHP5.6 in sequential call script 10,000 times SH test 10000 magic PHP5 get//Run data statistics script SH analysis./logs/get_magic_php5.log 10000//Results Avg:28μmmax: 211μmmin:22μm
PHP7.0 call script 10,000 times SH test 10000 no_magic PHP get//run data statistics script SH analysis./logs/get_no_magic_php.log 10000//Results avg:16μm max:295μmmin:10μm
PHP7.0 sequential call script 10,000 times sh test 10000 magic PHP get//run data statistics script SH analysis./logs/get_magic_php.log 10000//Results Avg:19μmmax:5 25μmmin:12μm
From the above data we can see:
The average time to execute a script that uses get in php5.6 is roughly equal to not using get, and the average time to execute the script with get in php7.0 is slower than not used, about 3 microseconds slower .
Conclusion
The main test here is construct (), call (), callstatic (), Get (), set (), which are the five common magic functions that can be replaced by other implementations. And come back to answer my doubts with the tests above.
Does the Magic method really have poor performance?
A: In addition to using construct, the time to use other magical methods is approximately 10 less subtle.
Is there a problem with the performance of the Magic method in PHP7?
A: The difference between using and not using magic methods in PHP7 is almost consistent in PHP5.6.
How should we reasonably use the Magic method?
A: Through the entire test we can see that the execution time difference between the non-use of magic method is roughly within 10 subtle, so if the Magic method can be very good to save our development costs and optimize our code structure, we should be able to consider sacrificing this less than 10 subtle. And construct is fast, so use construct should have no objection.