PHP Performance Analysis Magic Method sample sharing

Source: Internet
Author: User


Once remember that bird brother Laruence not recommended the use of "magic Method", since then once involved in the use of magic methods, bloggers will subconsciously think about it, so the photo is good? 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 the blogger to carry out in-depth study of the year, so now must be a close to this problem. Let's take a look at the bird Brother Laruence blog once mentioned:

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 uses Magic method data as follows, unit microseconds μm
10,000 consecutive calls to scripts in PHP5.6

SH Test 10000 Magic PHP5 construct//Run data statistics script SH analysis./logs/__construct_magic_php5.log 10000//Results Avg:28μmmax:896μmmi n:20μm

PHP7.0 does not use magic method data as follows, unit microsecond μ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//Results avg:19μmmax:819μmmin:13μm

PHP7.0 uses Magic method data as follows, unit microseconds μm
10,000 consecutive calls to scripts in PHP7.0

SH Test 10000 Magic PHP construct//run data statistics script SH analysis./logs/__construct_magic_php.log 10000//Results avg: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.

__call

Next, let's take a look at __call's experiment with the following PHP script:

<?php/** * Magic Method Performance Exploration * constructor * * @author Tigerb 

PHP5.6 uses Magic method data as follows, unit microseconds μ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μm max:392μmmin:22μm

PHP7.0 does not use magic method data as follows, unit microsecond μm

PHP7.0 consecutive 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//result avg: 16μmmax:256μmmin:10μm

PHP7.0 uses Magic method data as follows, unit microseconds μm

PHP7.0 sequential 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μmma x:2459μmmin:11μm

From the above data we can see:

The average time to execute a script using __call is slower than not 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 uses Magic method data as follows, unit microseconds μm

PHP5.6 in a 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 does not use magic method data as follows, unit microsecond μ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 uses Magic method data as follows, unit microseconds μm

PHP7.0 in sequential call script 10,000 times sh test 10000 magic PHP callstatic//run data statistics script SH analysis./logs/__callstatic_magic_php.log 10000// Result 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's experiment with the following PHP script:

<?php/** * Magic Method Performance Exploration * * Set private properties __set * * @author tigerb 

PHP5.6 uses Magic method data as follows, unit microseconds μm

PHP5.6 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μmma x:138μmmin:25μmphp7.0 does not use magic method data as follows, unit microsecond μm//PHP7.0 sequential call script 10,000 times SH test 10000 no_magic PHP set//run data statistics script SH analysis./log S/__set_no_magic_php.log 10000//Results avg:15μmmax:441μmmin:11μmphp7.0 use magic method data as follows, the unit microseconds μ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:120μ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 __get's experiment with the following PHP script:

<?php/** * Magic Method Performance Exploration * * Read Private properties __get * * @author tigerb 

PHP5.6 uses Magic method data as follows, unit microseconds μm

PHP5.6 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μmma x:211μmmin:22μm

PHP7.0 does not use magic method data as follows, unit microsecond μ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 μmmax:295μmmin:10μm

PHP7.0 uses Magic method data as follows, unit microseconds μm

PHP7.0 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: 525μ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 a script that uses __get in php7.0 is slower than not used, about 3 microseconds slower.

Conclusion

The main tests here are __construct (), __call (), __callstatic (), __get (), and __set (), which are commonly used and can be substituted for other magic functions. 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.

Once remember that bird brother Laruence not recommended the use of "magic Method", since then once involved in the use of magic methods, bloggers will subconsciously think about it, so the photo is good? 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 the blogger to carry out in-depth study of the year, so now must be a close to this problem. Let's take a look at the bird Brother Laruence blog once mentioned:

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

Related Article

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.