static method efficiency test code in PHP class

Source: Internet
Author: User
The class is defined as follows:
Copy CodeThe code is as follows:
Class Test
{
public static function A () {}
Public Function B () {}
}
$obj = new test;

Compare the following situations
Test::a ();
$obj->a ();
$obj->b ();
Test code:
Copy CodeThe code is as follows:
$obj = new test;
$test _times = 100;
$times = 10000;
$effi 1 = array ();
$effi 2 = array ();

while ($test _times--> 0)
{
$time 1 = microtime (true);
for ($i =0; $i < $times; $i + +)
{
Test::a ();
}
$time 2 = Microtime (true);
for ($i =0; $i < $times; $i + +)
{
$obj->a ();
}
$time 3 = Microtime (true);
for ($i =0; $i < $times; $i + +)
{
$obj->b ();
}
$time 4 = Microtime (true);
$effi 1[] = ($time 3-$time 2)/($time 2-$time 1);
$effi 2[] = ($time 4-$time 3)/($time 3-$time 2);
}
echo avg ($effi 1), "\ n", avg ($effi 2);

The last Avg is a function of the custom calculated average:
Copy CodeThe code is as follows:
function avg ($arr)
{
$result = 0;
foreach ($arr as $val)
{
$result + + $val;
}
$result/= count ($arr);
return $result;
}

Program Output results:
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

Repeat N (n>10) times, and this result is not a big difference, indicating:
1, directly through the class name access to static methods of efficiency is through the instance access static method 76%, even with PHP5.3 only 56%
2, the efficiency of accessing static methods through an instance is 106 of the efficiency of accessing non-static member methods, and in 5.3 the version becomes 110%
3, assuming that PHP from 5.2 to 5.3 through the class name access static method efficiency is not reduced, then the efficiency of the instance access function is increased by at least 35%. I have not read the PHP source code, has studied the PHP source code friend hoped can tell me this assumption is established (I think should be established)
Description: The above test based on Windows 7 and php.exe,5.2.14 use apache2.2 test results are not different, considering php.exe and through Web access to the implementation of the PHP core is the same, so 5.3 lazy to change the server configuration, 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.