Forum Moderators: coopster
As my sites grow, im starting to think that i should try and optimise the performance of my code more before it becomes a problem (especially when a hungry bot takes a couple of pages a second!).
Can anyone suggest any websites or books, that are targeted at someone wanting to improve the performance of their code. Alternatively could anyone post on common faults that hit performance?
thanks
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo printf("<div align=\"center\">page generated in %f seconds </div>", $totaltime);
After checking the generation time for the whole page, you should "drill down" to certain parts of the code, and see how long that takes. Usually you will find that 20% of the code is responsible for 80% of the page generation time. Those are the parts that you should work on. ("Forget the rest of the code." ;))
Common mistakes I see with self thaught/junior programmers is that they will make sql-queries in a while loop. As a rule by thumb it is usually better to fetch several rows with one query, and do the rest of the processing with PHP, but again in specific cases this may not hold true: so alway measure, measure, measure the time.
Also there are several ways to the same things in PHP, try out them out and see wchih is faster. For example str_replace is alot faster for simple replacements than preg_replace. However if you are going to substr() hundreds of lines of code into two carachter-batches and then do a str_replace, you can bet that preg_replace will be way better.;)
Some people will read a whole 1MB text file in an array, to output it. You might be better of reading it line by line, and stopping when you have what you got. You might read it in blocks of 64kb. You might fseek to a certain position and start reading from there on. You might simply want to use passthrough()...
To write efficient code is mostly experience, refactoring your won work, reading good code, and tweaking, trying, experimenting.
caching
After you have done that, you might want to start looking into caching if you have a very busy site. Caching can be done on very many different levels. If you send the right HTTP-Headers (last modified, expires, etag) then browsers and proxies will cache the outputted page.
If you have content that only changes once or twice a week, you might be better off to generate static pages upon every change you make, and your server will only have to serve static pages.
You can look into products that will cache the byte code of your scripts, so PHP won't have to reinterpret the code each time (drastic improvements can be achieved).
If you use templating´, you can "pre-compile" you templates saving you some work during page-generation-time. You can configure DB-Servers to cache the results of certain queries, or you can save those results in (heap-) tables yourself, only updating it once every hour, if the db-server is your bottleneck.
That's about all I can think of right now, but I guess this gives you something to work with for a while... :)