Comparison of static and non-static methods in php
If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly four times.
Practice is the only criterion for testing code execution efficiency. so I took a small test when I got home from work:
Test the code first (if there is something wrong, please correct it ):
Test environment:
/test # php -vPHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
/test # uname -aDarwin 192.168.0.101 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
System configuration can be viewed 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 result of the 10 million execution in a loop is as follows:
/Test #Php staticTest. php
2.2938342094421
1.1871800422668
Result of the second execution:
2.303295135498
1.1807670593262
After the execution is performed many times, it is near 2.3 and 1.18. It can be seen that although the efficiency improvement mentioned in the article is not so terrible, the performance improvement is indeed doubled.
Therefore, we recommend that you:
If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly doubled.
There are still a lot of classes and methods in a normal php Framework. if we can make static methods, it is better to use static methods and the efficiency is relatively high.