Efficiency towards PHP-accelerating your code execution speed the scripting language is inefficient, and PHP is no exception. The efficiency is actually saved or wasted in every line of your code. So here we will explain some basic efficiency knowledge to you, so that your code can be executed quickly. (Note: I have read the suggestions for optimizing PHP code in multiple versions of N. I think it is neither systematic nor comprehensive. here I just give some things I usually need to pay attention .) I. efficiency towards PHP-Speed up your code execution
Script language efficiency is low, and PHP is no exception. The efficiency is actually saved or wasted in every line of your code. So here we will explain some basic efficiency knowledge to you, so that your code can be executed quickly.
(Note: I have read the suggestions for optimizing PHP code in multiple versions of N. I think it is neither systematic nor comprehensive. here I just give some things I usually need to pay attention .)
I. string problems
1. the character concatenation is faster than the implode of the array than that of the sprintf.
You can execute the following code:
0; $i--) { $str .= 'String concatenation. '; } $end = microtime_float(); echo("
t i m e :" . round( $end - $start ,2) ."
"); $start=microtime_float(); // array join $str = ''; $sArr = array(); for ($i = 300000; $i > 0; $i--) { $sArr[] = 'String concatenation. '; } $str = implode('',$sArr); $end = microtime_float(); echo("
t i m e :" . round( $end - $start ,2) ."
"); ?>
?
The output result of my machine is:
T I m e: 0.14
T I m e: 0.25
2. string replacement
At the same time, if you cannot splice, consider replacement. The replacement method should take the following priority to consider writing code:
Sprintf is faster than str_replace than preg_replace is faster than strstr
3. string search, string comparison:
Some tests on the Internet are as follows:
Results
Ereg. 956
Preg_match. 050
Strstr. 222
Strpos. 033
Visible:
Strpos is faster than preg_match and strstr is faster than ereg.
Some people say that strstr is faster, but preg_match_all must be faster than strstr in the for loop. if it can be explode, it is faster than preg_match_all.
3. string output:
Echo is faster than print. However, if echo is used as quickly as possible?
$ Foo = 'John SMITH ';?
Echo "Hello $ foo, welcome on my blog .";?
Echo "Hello". $ foo. "welcome on my blog .";?
Echo 'hello'. $ foo. 'Welcome on my blog .';?
Echo 'hello', $ foo, 'Welcome on my blog .';
I think, you can understand, the last one is the fastest.
II. array problems:
Foreach is faster than. Not only that. If you use for, it is best to write it like this.
For ($ I = 0, $ j = count ($ array); $ I <$ j; $ I ++ ){
}
As mentioned above, the array is used for string concatenation, which slows down because you have gone through two cycles. However, many operations can be completed quickly with array assistance.
For example, array_mar ('trim', $ array) must be much faster than you write for and foreach.
You can use explode to split an array. it is best not to use strpos in the for loop.
Efficiency of the in_array function. If in_array is frequently used and the array is large, we recommend that you sort the array and use fast_in_array
This is a function added by users in the PHP Manual. (Note: the results to be tested, small array, in_array is still faster than it)
This function is five times faster than in_array (). It uses a binary search and shocould be able to be used as a direct replacement:
= $bot) { $p = floor(($top + $bot) / 2); if ($array[$p] < $elem) $bot = $p + 1; elseif ($array[$p] > $elem) $top = $p - 1; else return TRUE; } return FALSE; } ?>
?
Use arrays to change all of your controllable structures. This includes not only ternary operators, but also if and switch. Another advantage is that it can cultivate your thinking in the soft coding mode.
Instead
?? $ Class = 'even '? 'Odd': 'even'
We have
?? $ Flip = array ('even' => 'odd', 'odd' => 'even ');
?? $ Class = $ flip [$ class];
??
III. function problems
Use a positive name function. do not use a function alias. Aliases are used to promote PHP in PHP (for example, split, join is a function in VB, implode, explode is a positive name function), or for compatibility with the old version. Generally, the speed is not named fast.
??
Count is faster than sizeof
Is_integer is faster than is_int
Floatval is faster than doubleval
Implode is faster than join
Ini_set is faster than ini_alter
Of course, there are also a few exceptions, such as fputs being faster than fwrite. I think it can be ignored.