Tips for improving PHP efficiency

  Peter        2012-10-01 19:39:06       15,206        0    

0. Using single quote to replace double quote to enclose string literal, this will be a bit faster. Because PHP engine will search variables in double quoted string.

1. If a method in class can be declared as static, then make it static, this will be 4 times faster.

2. $row["id"] is 7 times faster than $row[id]

3. echo is faster than print, and you should use multiple parameters instead of string concatenation, i.e use comma(,) instead of dot(.) to concatenate string. For example echo $str1,$str2

4. Calculate the numer of loops before executing for loop, don;t calculate the number of loops in each loop. You'd better use foreach

5. unset those unused variables, especially arrays, to free memory

6. Avoid using __get,__set,__autoload

7. require_once() costs much

8. using abosulte path in include() because PHP engine will not seach include_path if absolute path is specified.
 
9. If you want to know the start time of script execution(The time server receives client request), use $_SERVER["REQUEST_TIME"] is better than time()
 
10. str_replace() is faster than preg_replace(), but strstr() is 4 times faster than str_replace()
 
11. Using switch case is better than using multiple if...else if statements
 
12. It's not efficient to use @ to hide error
 
13. Activate Apache's mod_deflate module can improve the webpage loading speed
 
14. Close database connection when finishing using it, avoid using persistent connection
 
15. Method defined in derived class is faster than the same method defined in base class
 
16. The speed to parse PHP script is twice to 10 times slower than parsing HTML. Try to use HTML
 
17. Try to use built-in function as much as you can
 
18. Don't use class to release all data structures, array is also very useful
 
19. Don't have too much file operations although the efficiency of file operation in PHP is not too low
 
20. If you have many time consuming functions, try to write C extensions to realize them
 
More to come.......
 
Author : 王大嘴 Source : http://cloudbbs.org/forum.php?mod=viewthread&tid=6415

PHP  EFFICIENCY  TIPS 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

X as a service

According to Chris Dixon, Every Unix command eventually become an internet service. Grep->Google, rsync->Dropbox, man->stack overflow, cron->ifttt ... sudo is NSA, LOL