Static and non-static methods in PHP efficiency comparison
Today, when I read this article, http://jingyan.baidu.com/article/6b97984d7892d21ca2b0bfc2.html found that the first sentence was this.
If you can define a method of a class as static, try to define it as static, and it will rise nearly 4 times times faster.
Practice is the only standard to test the efficiency of code execution, so while coming home from work a little time, did a small test:
Test the code first (if there is a wrong place, please correct it):
Test environment:
/test # php-vphp 5.4.24 (CLI) (Built:jan 21:32:15) Copyright (c) 1997-2013 the PHP groupzend Engine v2.4.0, Copy Right (c) 1998-2013 Zend Technologies
/test # Uname-adarwin 192.168.0.101 13.1.0 Darwin Kernel Version 13.1.0:thu Jan 19:40:37 PST 2014; Root:xnu-2422.90.20~2/release_x86_64 x86_64
System configuration can be seen in this article: http://blog.csdn.net/lamp_water/article/details/20968155
Test ();} Echo (Microtime (True)-$start). " \ n "; $start = Microtime (True); for ($i =0; $i <10000000; $i + +) { statictest::teststatic ();} Echo Microtime (True)-$start;
The results of the 10 million execution loop execution are as follows:
/test # PHP statictest.php
2.2938342094421
1.1871800422668
Second execution Result:
2.303295135498
1.1807670593262
Executed many times later, all around 2.3 and 1.18. Visible, although no article said the efficiency of the increase is so scary, the performance of the improvement aspect did increase by about one times.
It is therefore recommended that:
If you can define a method of a class as static, try to define it as static, and it will rise nearly 1 time times faster.
For a normal PHP framework, the classes and methods inside are still very large, if we can make static method, or use static method is better, efficiency is higher.