Kenneth Downs wrote: > Dan Cech wrote: >> Nelly Yusupova wrote: >> >>> I just read 40 Tips for optimizing your php code post and wanted to >>> share it >>> with you all: >>> >>> http://reinholdweber.com/?p=3 >> >> Definitely an interesting article, but a little short-sighted. > > I would say incomplete, or perhaps cart-before-horse. > > As many have pointed out, the big two are database optimization and size > of return payload. Everything else is single-digit percentages after that. > > Once you've got the db stuff down though, it seems it can't hurt to > develop habits to use code known to be slightly faster, like the > single-quote vs. double-quote thing.
Absolutely there are tips in there which are valuable, but by the same token there are many which are of marginal value. For example, the 'tip' regarding checking the length of a string (33). While the method advocated may save a couple of cycles, it adds another level of obscurity to the code which will make it that much less readable and maintainable in the future, especially if you are not the one doing the maintaining. The tips I would consider worthwhile are: 5. Unset your variables to free memory, especially large arrays. This is a good practice and can help out greatly if you are operating on large data sets. 15. Turn on apache's mod_deflate 42. 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% These are really the same tip, compressing output can increase speed if your pages are large or your clients are on slow connections. 16. Close your database connections when you're done with them If you are using non-persistent connections this is a good idea and will help you to avoid having old connections hanging around on the db server. If you're running on a dedicated machine persistent connections are even better (don't use persistent connections on a machine hosting multiple different applications). 17. $row['id'] is 7 times faster than $row[id] This is elementary, and if you are developing with error_reporting E_ALL (as you should) you would know that php will try to resolve id as a constant if it is not quoted. 28. Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside '...'. Of course you can only do this when you don't need to have variables in the string. I subscribe to the single quotes school of thought, mostly because I find it much easier to locate variables when reading the code. Also, if the string you are building is destined for html, sql or a url the variables should be escaped anyway. Using single-quotes and escaping inline makes it trivial to spot where this isn't being done. $name = $_GET['name']; // 10 lines of code echo 'Your Name: '. htmlentities($name); vs $name = htmlentities($_GET['name']); // 10 lines of code echo "Your Name: $name"; If you get into the habit of using the first format (I define a function h() as a shortcut for htmlentities()) you will be able to spot instantly any place you forget to properly escape $name. With the second format I guarantee you won't. 31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times. 32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request Good advice, and can definitely provide huge speed increases. 41. 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 This is the kicker, and will tell you where YOU can make gains with YOUR code. If you want more speed, start by profiling your code and you will be able to tell exactly where to focus your efforts for maximum results. Dan _______________________________________________ New York PHP Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php