Pp

514 views

Published on

php optimization

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
514
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 05/19/12 phpoptimization php optimization
  • Pp

    1. 1. 05/19/12 php optimization 1
    2. 2.  There are many ways to improve the way you write your PHP code.  And we can easily increase the efficiency of our code just by putting in some effort during development.  However, there might be some unknown information that you might not aware in PHP that can help improve your code.  In php optimization concepts looks more efficient than other things. They are followings in next slides,05/19/12 php optimization 2
    3. 3. ECHO VS PRINT:  Echo is better.  But how much better?  Its around 12%-20% faster using echo compare to print when there is no $ symbol in the printing string.  And around 40-80% faster if there is an $ symbol used in a printing string! SINGLE VS DOUBLE QUOTES:  Single(’) quote is faster than double(”) quote.  Why? Because PHP will scan through double quote strings for any PHP variables (additional operation).  So unless you have a $var inside the string use single quotes.05/19/12 php optimization 3
    4. 4. Ex:1 $var = I have text; $var2 = "I have text"; // We dont have vars so single would be good; $var3 = "I have $var"; // In this case the double quotes is necessary LOOP:  Loop is considered as efficiency killer if you have many nested loop (means loop in a loop) as one loop will required to run ‘n’ times  If you have 1 nested loop, this means your program will have to run n2 times.  Using a for loop is better than foreach and while loop if the maximum loop is pre-calculated outside the for loop!05/19/12 php optimization 4
    5. 5. Ex:2 // For each loop the count function is being called. for($i =0; $i < count($array);$i++) { echo This is bad; } #Better than foreach and while loop $total = (int)count($array); for($i =0; $i < $total;$i++) { echo This better, max loop was pre-calculated; } DOT VS COMMAS CONCATENATION: $a = 10 PHP programming ; $b = Improvement Tips; #10 PHP Programming Improvement Tips echo $a.$b;05/19/12 php optimization 5
    6. 6. The other way is: $a = PHP ; $b = Tips; echo $a,$b;  Tests show that dot is more preferable if there are no variables or $ symbol involved which is around 200% faster.  On the other hand, commas will help to increase around 20%-35% efficiency when dealing with $ symbols. PRE INCREMENT VS POST INCREMENT:  In PHP, it seems like pre increment is better than the other ways of performing an increment.  Its around 10% better than post increment? The reason? Some said that post increment made certain copy unlike pre increment.05/19/12 php optimization 6
    7. 7. EX:3 $i++; ++$i; $i+=1; $i = $i + 1; EXPLODE VS PREG_SPLIT:  To split a string the usual way is to use explode because it support even on PHP4.0. The answer in term of efficiency is explode.  Split supports regular express and this makes it quite the same comparison between str_replace and preg_replace, anything that have regular expression support will usually be a bit more slower than those that doesn’t support it.  It took around 20.563% faster using explode in PHP.05/19/12 php optimization 7
    8. 8. WHEN CHECKING THE LENGTH OF STRINGS: Use isset where possible in replace of strlen. if (!isset($foo{5})) { echo "Foo is too short"; } //is faster than if (strlen($foo) < 5) { echo "Foo is too short"; }05/19/12 php optimization 8
    9. 9. VARIABLES AND FUNCTIONS  There are some handy things you can do with variables and functions in php to help optimize your script.  Unset or null your variables to free memory, especially large arrays. Use require() instead of require_once() where possible.  Use absolute paths in includes and requires. It means less time is spent on resolving the OS paths. include(/var/www/html/your_app/test.php); //is faster than include(test.php);  require() and include() are identical in every way except require halts if the file is missing. Performance wise there is very little difference.05/19/12 php optimization 9
    10. 10. “else if” statements are faster than “switch/case” statements. PHP OPTIMISATION TIPS REVISITED:  Avoid magic like __get, __set, __autoload.  Since PHP5, the time of when the script started executing can be found in $_SERVER[’REQUEST_TIME’], use this instead of time() or microtime().  When parsing with XML in PHP try xml2array, which makes use of the PHP XML functions, for HTML you can try PHP’s DOM document or DOM XML in PHP4.  str_replace is faster than preg_replace, str_replace is best overall, however strtr is sometimes quicker with larger strings. Using array() inside str_replace is usually quicker than multiple str_replace. [05/19/12 php optimization 10
    11. 11.  Close your database connections when you’re done with them.  $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.  Use <?php … ?> tags when declaring PHP as all other styles are depreciated, including short tags.  When using header(‘Location: ‘.$url); remember to follow it with a die(); as the script continues to run even though the location has changed or avoid using it all together where possible.  Incrementing a local variable in an OOP method is the fastest. Nearly the same as calling a local variable in a function and incrementing a global variable is 2 times slow than a local variable.05/19/12 php optimization 11
    12. 12.  Methods in derived classes run faster than ones defined in the base class.  If you need to find out the time when the script started executing, $_SERVER[REQUEST_TIME] is preferred to time()  Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable. true is faster than TRUE:  This is because when looking for constants PHP does a hash lookup for name as is.  And since names are always stored lowercased, by using them you avoid 2 hash lookups. Furthermore, by using 1 and 0 instead of TRUE and FALSE, can be considerably faster.05/19/12 php optimization 12
    13. 13. select vs. multi if and else if statements:  It’s better to use select statements than multi if, else if statements.05/19/12 php optimization 13

    ×