Because there are several projects that are waiting to be done, and they are not very urgent, and the frameworks that have been developed are more or less difficult to use, I decided to write a framework first and then do the project again. Since the framework is written, it is natural that some execution efficiency tests are often performed. Today, a static efficiency test is conducted. The class is defined as follows: classtest {publicstaticfunctiona () {} publicfu is not very urgent because there are several projects waiting to be done. In addition, it is always a bit unsatisfactory to use the frameworks that have been developed at present, so I decided to write a framework first and then create a project. Since the framework is written, it is natural that some execution efficiency tests are often performed. Today, a static efficiency test is conducted.
The class is defined as follows:
class test{ public static function a(){} public function b(){}}
Compare the following situations
- Test: ();
- $ Obj-> ();
- $ Obj-> B ();
Test code:
$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 final avg is a function of the custom average calculation:
function avg($arr){ $result = 0; foreach ($arr as $val) { $result += $val; } $result /= count($arr); return $result;}
Program output result:
PHP 5.2.14
0.764906288480911.0699484376399
PHP 5.3
0.56919482299058
1.1016495598611
Repeated N (N> 10) times are not much different from the results. note:
- 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.
- 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%.
- If the efficiency of accessing static methods through class names is not reduced 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.