Test code for Static method efficiency in PHP

Source: Internet
Author: User

The class is defined as follows:
Copy codeThe Code is as follows:
Class test
{
Public static function (){}
Public function B (){}
}
$ Obj = new test;

Compare the following situations
Test: ();
$ Obj-> ();
$ Obj-> B ();
Test code:
Copy codeThe Code is as follows:
$ Obj = new test;
$ Test_times = 100;
$ Times = 10000;
$ Effi1 = array ();
$ Effi2 = array ();

While ($ test_times --> 0)
{
$ Time1 = microtime (true );
For ($ I = 0; $ I <$ times; $ I ++)
{
Test: ();
}
$ Time2 = microtime (true );
For ($ I = 0; $ I <$ times; $ I ++)
{
$ Obj-> ();
}
$ Time3 = microtime (true );
For ($ I = 0; $ I <$ times; $ I ++)
{
$ Obj-> B ();
}
$ Time4 = microtime (true );
$ Effi1 [] = ($ time3-$ time2)/($ time2-$ time1 );
$ Effi2 [] = ($ time4-$ time3)/($ time3-$ time2 );
}
Echo avg ($ effi1), "\ n", avg ($ effi2 );

The final avg is a function of the custom average calculation:
Copy codeThe Code is as follows:
Function avg ($ arr)
{
$ Result = 0;
Foreach ($ arr as $ val)
{
$ Result + = $ val;
}
$ Result/= count ($ arr );
Return $ result;
}

Program output result:
Copy codeThe Code is as follows:
PHP 5.2.14
View sourceprint? 1 0.76490628848091
2 1.0699484376399
View sourceprint? 1. PHP 5.3
View sourceprint? 1 0.56919482299058 <BR> 1.1016495598611

Repeated N (N> 10) times are not much different from the results. Note:
1. The efficiency of directly accessing static methods through class names is 76% of the Efficiency of accessing static methods through instances, or even only 56% when PHP5.3 is used.
2. The efficiency of access to static methods through instances is 106 of the Efficiency of access to non-static member methods, and changed to 5.3 in version 110%.
3. If the efficiency of accessing static methods by class name is not reduced when PHP is upgraded from 5.2 to 5.3, the efficiency of accessing functions through instances is improved by at least 35%. I have not read the PHP source code, and I have studied the PHP source code. I hope to tell me whether this assumption is true (I think it should be true)
Note: The above test is based on windows 7 and php.exe,5.2.14use apache2.2to test the result. The PHP core executed by accessing php.exe is the same as that of web access, so 5.3 is too lazy to change the server configuration, and the result should be the same.

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.