PHP Magic Function Performance code detailed analysis

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 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

    1. Does the Magic method really have poor performance?

    2. Is there a problem with the performance of the Magic method in PHP7?

    3. 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 does not use magic method data as follows, unit microsecond μm

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 uses Magic method data as follows, unit microseconds μm

PHP5.6 in sequential call script 10,000 times SH test 10000 magic PHP5 construct//Run data statistics script SH analysis./logs/__construct_magic_php5.log 10000// Result avg:28μmmax:896μ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 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

PHP7.0 consecutive call script 10,000 times 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 does not use magic method data as follows, unit microsecond μm

PHP5.6 in sequential 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//Results av g:27μmmax:206μmmin:20μm
    • 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 does not use magic method data as follows, unit microsecond μm

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 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 that uses __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 does not use magic method data as follows, unit microsecond μm

PHP5.6 consecutive 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//result avg: 31μmmax:110μmmin:24μm
    • 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μ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 set//run data statistics script SH analysis./logs/__set_no_magic_php.log 10000//Results avg:15 μmmax:441μmmin:11μm
    • PHP7.0 uses Magic method data as follows, unit microseconds μm

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

PHP5.6 consecutive 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//result avg: 28μmmax:590μmmin:20μm
    • 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.

    1. 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.

    1. 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.

    1. 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.

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.