Efficiency to PHP-speed up your code execution

Source: Internet
Author: User
Tags ereg
Efficiency to PHP-speed up your code execution

scripting language efficiency is low, PHP is no exception. The efficiency is actually saved or wasted in every line of your code. So, here are some basic efficiency tips to get you up and running faster.
(Note: Read the recommendations of the N multi-version optimization of PHP code, feel that the system is not comprehensive, this is just to give a few of the usual needs to pay attention to. )

First, string problems

1, the character concatenation is bigger than the array implode, also faster than 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 of my machine is:

t i m e:0.14

t i m e:0.25

2. String substitution

At the same time, if not splicing, consider replacing. Instead, consider writing the code in the following priority way:

sprintf faster than str_replace faster than preg_replace faster than strstr

3. String lookup, string comparison:

The results of the online test are:

Results
Ereg. 956
Preg_match. 050
Strstr. 222
Strpos. 033
Visible:
Strpos faster than preg_match faster than strstr faster than Ereg
Some people say, strstr fast, but, preg_match_all must be faster than the STRSTR for the For loop, if it can explode, it is faster than Preg_match_all

3. String output:
echo is faster than print, and that's not the story. But what if you use echo the fastest?

$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.


Second, array problems:

foreach is faster than for this is what we all know. Not only this. If you really use for, you write the best.

For ($i =0, $j =count ($array), $i < $j; $i + +) {
}

As I said earlier, arrays are used to make string concatenation, which will be slow because you go through two loops. But a lot of operations, if can be done with array assistance, will be very fast.
For example: Array_mar (' trim ', $array) must be much faster than you write For,foreach.
It is best not to use the Strpos in the For loop if you can first split the array with explode.

The efficiency problem of in_array function. If In_array is used frequently and the array is large, it is recommended to sort this array and then, with Fast_in_array

This is a user-added function in the PHP manual. (Note: Pending test result, decimal group, In_array or faster than it)
This function is five times faster than In_array (). It uses a binary search and should is 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;    }         

?

Use an array to change all the control structures you can change. This includes not only ternary operators, but also: If,switch. There is another benefit to thinking about developing your soft coding patterns.

Instead of

?? $class = $class = = = ' Even '? ' Odd ': ' Even '

We have

?? $flip = Array (' even ' = ' odd ', ' odd ' = ' even ');
?? $class = $flip [$class];
??
Three, function problem

Use the justify function instead of the alias of the function. Aliases are used in PHP for the promotion of PHP (for example, Split,join is a function in VB, Implode,explode is the name of the function), or used to be compatible with older versions. The general speed does not justify the fast.
??
Count faster than sizeof
Is_integer faster than Is_int
Floatval faster than Doubleval
Implode faster than join
Ini_set faster than Ini_alter

Of course, there are very few exceptions, such as: fputs faster than Fwrite, I think, can ignore it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.