Home >Backend Development >PHP Tutorial >Static method efficiency test code in PHP class_PHP tutorial

Static method efficiency test code in PHP class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:56859browse

The definition of the class is as follows:

Copy code The 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 code The 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::a();
}
$time2 = microtime(true);
for($i=0; $i<$times; $i++)
{
$obj->a();
}
$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 last avg is a custom function to calculate the average:
Copy code Code As follows:

function avg($arr)
{
$result = 0;
foreach ($arr as $val)
{
$result + = $val;
}
$result /= count($arr);
return $result;
}

Program output result:
Copy code The 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 execution N (N>10) times, the result is not much different from this, explanation:
1. The efficiency of accessing static methods directly through class names is 76% of that of accessing static methods through instances, even when using PHP5.3 it is only 56%
2. The efficiency of accessing static methods through instances is 106% of the efficiency of accessing non-static member methods. It becomes 110% in version 5.3
3. Assuming that the efficiency of accessing static methods through class names does not decrease when PHP is upgraded from 5.2 to 5.3, then the efficiency of accessing functions through instances has increased by at least 35%. I have not seen the PHP source code. Friends who have studied the PHP source code 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.14 is used There is no difference in the apache2.2 test results. Considering that php.exe and the PHP core executed through web access are the same, so 5.3 is too lazy to change the server configuration, and the results should be the same.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322524.htmlTechArticleThe definition of the class is as follows: Copy the code The code is as follows: class test { public static function a(){} public function b (){} } $obj = new test; Compare the following situations test::a(); $obj-a(); $ob...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn