This article is about PHP in some of the skills to mention high performance, has a certain reference value, now share to everyone, the need for friends can refer to
Some tips for high performance in PHP
Introduction: PHP As a scripting language, its own performance is certainly not as good as C + + or java. With easy-to-learn features, there is no room for performance improvement. Develop some good programming habits that may allow your PHP code performance to be visually enhanced.
First, eliminate unnecessary error prompts
There are many friends in the programming of the time encountered notice and warning such errors, if not affect the normal logic will not be processed, similar to the following
<?php//Want to stitch strings in loops, but not initialize strings directly. = $list = Array (1=> ' hello ', 2=> ' world '//... ); foreach ($list as $key = = $val) {$str. = $val; }//notice:undefined variable:str in D:\11\index.php/*********************************************************/ Unnoticed array subscript is out of bounds or key does not exist $List _1 = Array (' 1 ', ' 2 '); echo $List _1[3]; notice:undefined Offset:3 in D:\11\index.php on line 13/*********************************************************/ Using obsolete functions such as using function mysql_escape_string () will have the following hint//deprecated:mysql_escape_string (): This function is Deprecated; Use Mysql_real_escape_string () instead. In D:\readCode\xhprofshoujikanbingcom\cgi\xhprof.php on line 51/************************************************** ////Static call non-static method E_strictclass cl_a{function A () {echo ' A ' Class A method '; }}class cl_b{static function B () {echo ' B ' class B method '; }}function test_1 () {$a = new cl_a (); $a:: A ();} function TeSt_2 () {$b = new cl_b (); $b:: B ();} Test_1 (); test_2 (); Strict Standards:non-static Method Cl_a::a () should not being called statically in D:\11\index.php on line 15?>
This situation can lead to degraded performance, and any code that can cause a warning (warning or e_strict) or hint (notice) will go through the exception flow in PHP, logging the Exception log (involving file I/O). Performance may be reduced by about 50%.
Ii. using static variables and methods
The test results are as follows
<?phpclass cl_a{public $a =1; function A () { $this->a++; }} Class cl_b{public static $a = 1; static function B () {Self :: $a + +; }} function test_1 () { $a = new cl_a (); for ($i =0; $i <1000; $i + +) { $c = $a->a+1;//external call $a->a ();//Internal call } echo $a->a;} function test_2 () { $b = new Cl_b (); for ($i =0; $i <1000; $i + +) { $c = $b:: $a +1;//external call $b:: B ();//Internal call } echo $b:: $a;} Test_1 (); 51012 μs test_2 (); 49039 μs?>
Third, try not to apply the @ symbol
Honestly, I've never seen a place where you have to use the @ symbol.
<?phpclass cl_a{public $a =1; function A () { $b =1; }} function test_1 () { @ $a = new Cl_a (); for ($i =0; $i <1000; $i + +) { @ $a->a ();//Internal call } echo $a->a;} function test_2 () { $a = new cl_a (); for ($i =0; $i <1000; $i + +) { $a->a ();//Internal call } echo $a->a;} @test_1 (); 51,133test_2 (); 48,381?>
Use PHP built-in variables to replace some functions.
You can also replace time () with $_server[' Request_time ' at some point. 、
The performance of the tips I've tested the results of I have a little to believe
<?phpfunction test_1 () {for ($i =0; $i <1000; $i + +) { $a = php_uname (' s '); $b = Phpversion (); $c = Php_sapi_name (); } echo $a, $b, $c;} function Test_2 () {for ($i =0; $i <1000; $i + +) { $a = Php_os; $b = php_version; $c = Php_sapi; } echo $a, $b, $c;} Test_1 (); 132,015 test_2 (); 340 surprised not surprise $is_win = Directory_separator = = '//';//can be used to determine if the Windows system is fast and fast?>
V. Be careful not to write unnecessary time-consuming code into the loop
For example, the Cuont function should not be written in the conditions of a for loop, do not declare unnecessary variables in the loop, and so on.
<?phpfunction test_1 () { $a = array ( 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, ); for ($i =0; $i <count ($a), $i + +) { }}function test_2 () { $a = array ( 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0, ); $count = count ($a); for ($i =0; $i < $count; $i + +) { }}test_1 ();//3,602test_2 ();//223?>
Six, try to replace the regular with PHP built-in functions
This online has a lot of contrast, I will not re-write again;
Give a straight look at the common three function performance comparison: str_replace > Strtr > Preg_replace
Seven, contains the document has the skill
When you include a file, if you can be sure that it will not be duplicated, try to use Include,require instead of include_once and require_once, and include errors (generally you function and variables are overwritten) can usually be tested.
The test results are as follows:
<?phpfunction test_1 () { $a = ' one '; for ($i =0; $i <1000; $i + +) { include_once ' a.php '; }} function test_2 () { $a = ' one '; for ($i =0; $i <1000; $i + +) { include ' a.php '; }} Test_1 ();//1,477test_2 ();//152,704?>
Eight, you can use the whole equal sign instead of double
This is believed to be known by a lot of people because the double equals sign has a type conversion.
The test results are as follows:
<?phpfunction test_1 () { $a = ' one '; for ($i =0; $i <1000; $i + +) { if ($a = = ') { echo 1; }} } function test_2 () { $a = ' one '; for ($i =0; $i <1000; $i + +) { if ($a = = = ') { echo 1; }} } Test_1 ()///Time 501 microseconds test_2 ();//434 microseconds?>
Nine, multidimensional array assignment can use references to improve performance
The more complex the multidimensional array, the greater the performance of the reference, the more the reference can reduce the hash lookup times of the array elements.
<?phpfunction test_1 () { $a [' B '] [' c '] = Array (); $a [' B '] [' d '] = Array (); for ($j =0; $j <1000; $j + +) {for ($i = 0; $i < 5; + + $i) { $a [' B '] [' C '] [$i] = $i; }} } function test_2 () { $a [' B '] [' c '] = Array (); $a [' B '] [' d '] = Array (); $ref =& $a [' B '] [' C ']; for ($j =0; $j <1000; $j + +) {for ($i = 0; $i < 5; + + $i) { $ref [$i] = $i; }} } Test_1 ();//1270test_2 ();//1015//the more complex the multidimensional array, the greater the performance of the reference, the more the reference can reduce the hash lookup of the array element?>
Ten, large array or data if the use is complete, timely unset off
This will not have to explain too much, save memory.
Xi. do not make a meaningless package
If you cannot achieve a particularly good design, decoupling, the reuse effect can not encapsulate a simple method
Because each invocation of the method opens up a new memory area, the reference to the data is incremented when the value is passed.
12. Caching for code and data
This data cache is needless to say, there may be some friends do not know the code cache, code cache saves code run time away from: PHP is interpreted language, and the compiler language differences are as follows:
compiled languages : After programming, after the completion of the program, the compilation---------and the link becomes an executable program (the computer directly runs the binary file), each subsequent run will run the executables directly.
interpreted language : The specific process is not clear here, it can be understood that the interpretation of the language each time it runs the same as the compilation of the above compiler language (in fact, not quite the same) to generate executable files.
Opcache is to put the explanation of the file into memory, each run without the interpretation of the process, you can improve the performance of 20%-100% (data from the network, but the principle is certainly able to improve performance, if the code is often iterative to use caution).
13. Replace double quotation marks with single quotation marks
Because the internal variables can be placed inside the double quotes, PHP will determine if there are any variables inside.
<?phpfunction test_1 () { $a = " 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 "; for ($i =0; $i <1000; $i + +) { echo $a; }} function test_2 () { $a = ' 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 '; for ($i =0; $i <1000; $i + +) { echo $a; }} Test_1 ();//2953test_2 ();//2025?>
About foreach and for loop efficiency
This article has been too long, about the efficiency of foreach and for loops and the effectiveness of file reading next time, let's introduce it separately.