PHP class static method efficiency test Code _php Tutorial

Source: Internet
Author: User
Tags php source code
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 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
1.1016495598611

Repeated N (n>10) times, and this result is not significant difference, indicating:
1, directly through the class name access to the static method of the efficiency is through the instance access to the static method of 76%, even when using PHP5.3 only 56%
2. The efficiency of accessing the static method through an instance is 106 of the efficiency of accessing the non-static member method, and the 5.3 version becomes 110%
3, assuming that PHP from 5.2 to 5.3 when accessing the static method through the class name does not reduce the efficiency, then the efficiency of accessing the function through the instance increased by at least 35%. I have not read the PHP source code, have studied the PHP source of the friend hope to tell me whether this hypothesis is set up (I think it should be set up)
Description: The above test is based on Windows 7 and php.exe,5.2.14 use apache2.2 test results no difference, considering Php.exe and through the Web Access to execute the PHP core is the same, so 5.3 too lazy to change the server configuration, the result should be the same.

http://www.bkjia.com/PHPjc/322524.html www.bkjia.com true http://www.bkjia.com/PHPjc/322524.html techarticle the definition of the class is as follows: The copy code code is as follows: Class Test {public static function A () {} public Function B () {}} $obj = new test; Compare the following cases test::a (); $obj-A (); $ob ...

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