Like any scripting language, PHP can be used in a variety of applications. The down-side for most programmers is that when they learn how to write PHP, they do not always learn how to write PHP with speed and optimization in mind. This article addresses most common ways you can improve your code with minor changes that will yield large gains as well as teach you how to become a better PHP developer.
One of the most important things you can do when improving PHP application performance is begin measuring your application to find a baseline performance time for different parts of your application. If you do not know which parts of your application are slow or have bottlenecks, you will not know where to begin optimizing your code for better performance. There are many commercial and free applications that can be used to measure PHP performance.
1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
2. echo is faster than print, use echo’s multiple parameters instead of string concatenation.
3. Set the max value for your for-loops before and not as a function in the loop:
$len = count($big_array); for ($i=0; $i<$len; $i++) {...}
4. Unset your variables to free memory, especially large arrays.
5. Avoid magic methods like __get(), __set() and __autoload()
6. To find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()
7. See if you can use strncasecmp, strpbrk and stripos instead of regex
8. strtr is faster than str_replace by a factor of 4
9. It’s better to use select statements (switch() { case ”: }) than multi if and else if statements.
10. Error suppression with @ is very slow.
11. Turn on apache's mod_deflate
12. mod_gzip which is available as an Apache module, compresses your data on the fly and can reduce the data to transfer up to 80%
13. Close your database connections when you’re done with them(although PHP does it for you upon full page load or end of script execution)
14. Use single quotes in indexes: $row['id'] is 7 times faster than $row[id]
15. Use sprintf instead of variables contained in double quotes, it’s about 10x faster.
16. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
17. Incrementing a global variable is 2 times slower than a local var.
18. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
19. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
20. Just declaring a global variable without using it in a function also slows things down (by the same amount as incrementing a local var).
PHP does a pricy check to see if the global exists.
21. Methods in derived classes run faster than ones defined in the base class.
22. A function call with one parameter and an empty function body takes about the same time as doing 7-8$localvar++ operations.
A similar method call is of course about 15 $localvar++ operations.
23. Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside "...".
24. When working with strings and you need to check that the string is of a certain length,
you'd understandably would want to use the strlen() function. This function is pretty
quick since it’s operation does not perform any calculation but merely returns
the already known length of a string available in the zval structure (internal C struct used
to store variables in PHP). However because strlen() is a function it is still somewhat slow
because the function call requires several operations such as lowercase & hashtable lookup
followed by the execution of said function. In some instance you can improve the speed of your
code by using anisset() trick.
if (!isset($foo{5})) { echo 'Foo is too short'; } ?>
instead of
if (strlen($foo) < 5) { echo 'Foo is too short'; } ?>
25. Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. You’ll have no overhead on top of the actual code that determines the string’s length.
26. When incrementing or decrementing the value of the variable $i++ happens to be a much slower then++$i. It is because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes the creation of a temporary var that is then incremented while pre-incrementation increases the original value directly. This is one of the optimization that opcode optimizer such as Zend’s PHP optimizer utilizes.
27. Use array_pad(). A common case when assigning multiple variables from an array is to use the list()construct with the explode() function to split the string and set the parts of the list to each variable like so:list($foo1, $foo2, $foo3) = explode('/', 'var1/var2/var3'); However, the problem is then created where you may not be bringing in as many variables for each part of the list. If you are missing a variable or two when you explode, you will get a nice notice from PHP. The usual response to this is to use the error suppression operator to dismiss the error (@list()). In this case, $foo3 will be set toNULL and your script will suffer a speed penalty for use of the @ operator. To alleviate this problem it is better to use array_pad to only fill the array with the number of variables that you have instead of causing and suppressing an error at the cost of your performance.
28. list($foo1, $foo2, $foo3) = array_pad(explode('/', 'var1/var2'), 4, NULL);
29. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
30. Do not implement every data structure as a class — arrays are useful too
31. If you have very time consuming functions in your code, consider writing them as C extensions
32. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview
33. Excellent article about optimizing your PHP code by John Lim
34. Do use foreach for looping collections/arrays. PHP4 items are byval, < PHP5 items are byref
35. Do consider using the Singleton Pattern as you will have only one instance of an object.
36. Do use POST over GET for all values that will wind up in the database for TCP/IP packet performance reasons.
37. Do use ctype_alnum, ctype_alpha and ctype_digit over regular expression to test form value types for performance reasons.
38. Do use full file paths in production environment over basename/file_exists/open_basedir to avoid performance hits for the filesystem having to hunt through the file path. Once determined, serialize and/or cache path values in a $_SETTINGS array. $_SETTINGS['cwd'] = cwd(./);
39. Do use require/include over require_once/include_once to ensure proper opcode caching.
40. Do use tmpfile or tempnam for creating temp files/filenames
41. Do use a proxy to access web services (XML or JSOM) on foreign domains using XMLHTTP to avoid cross-domain errors. eg. foo.com<–>XMLHTTP<–>bar.com
42. Do use error_reporting(E_ALL); during debug.
43. Do set Apache allowoverride to “none” to improve Apache performance in accessing files/directories.
44. Do use a fast fileserver for serving static content (thttpd) static.mydomain.com, dynamic.mydomain.com
45. Do serialize application settings like paths into an associative array and cache or serialize that array after first execution.
46. Do use PHP output control buffering for page caching of heavily accessed pages
47. Do use PDO (or a major PHP framework such as CakePHP, Symfony, CodeIgniter, Yii or phpiphany) for prepared statements over native DB prepare. mysql_attr_direct_query=>1
48. Do NOT use SQL wildcard selects. eg. SELECT * (although selecting all columns manually yields same retrieval time as issuing SELECT *; therefore try to select only the columns you are actually going to use)
49. Do use database logic (queries, joins, views, procedures) over loopy PHP.
50. Use caching whenever possible. This article defines basic caching techniques. To sum it up, use arrays for caching big chunk of data in the memory directly. Use APC or eAccelerator on a single server. Use memcache on multiple servers when you scale you application.
51. APC being the fastest caching method does not scale well. Redis and Memcache can be used for cross-process/multi-server communications. Data in Redis storage will remain even after a server reboot. Don’t want to install Redis (it’s just a 1 line in console)? Use Memcache. If you can’t install any third-party packages, you can use Shared Memory – but your PHP should be compiled with support of shmop-functions. If we take APC as our unit of measure in the following benchmark, we can draw the following conclusion:
APC – best performance – speed 1
Redis – speed 1.6
Shared memory – speed 130
Memcache – speed 192 (slowest)
0 comments:
Post a Comment