Home arrow PHP arrow Page 4 - Optimizing System Performance

Keep Your Code Simple - PHP

In this second part of a two part series, you'll learn how to use debuggers and optimize performance. It is excerpted from chapter 12 of the book Zend PHP Certification, written by George Schlossnagle et al (Sams; ISBN: 0672327090).

TABLE OF CONTENTS:
  1. Optimizing System Performance
  2. Optimizing Performance
  3. Database Optimizations
  4. Keep Your Code Simple
  5. Exam Prep Questions
By: Sams Publishing
Rating: starstarstarstarstar / 8
November 30, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you're coming from a Java background, you might be used to writing a large infrastructure of classes that rely on each other to perform a particular task.

Don't try this with PHP! PHP's OOP features work best when your framework is small and efficient. Creating objects in PHP is a rather slow process, and, as such, it should be used conscientiously.

Caching Techniques

Sometimes, it's just not possible to optimize your code beyond a certain point. It might be that your queries are too complicated or that you depend on a slow external resource, such as a web service, over which you have no control.

In these cases, you might want to think about using a caching solution that "saves" the output of an operation and then allows you to access it without performing that operation again.

There are several types of cache; for example, you can save the results of a database query, or even an entire web page. The latter means that you generate your pages normally at predetermined intervals and save them in the cache. When a page is requested by a user, it is actually retrieved from the cache instead of being generated from scratch.

You can find several packages in the PEAR repository that are useful for output caching of various type. Naturally, there are also commercial solutions that perform a similar task, such as the ones provided by Zend.

Bytecode Caches

When PHP runs your scripts, it does so in two steps. First, it parses the script itself, transforming it into a sort of intermediate language referred to as bytecode. Then, it actually interprets the bytecode (which is simpler than PHP itself) and executes it. If your scripts don't change between one execution and the next, the first step could easily be skipped, and only the second step would have to be taken.

This is what "bytecode caches" do. They are usually installed as simple extensions to PHP that act in a completely transparent way, caching the bytecode versions of your script and skipping the parsing step unless it is necessary—either because the script has never been parsed before (and, therefore, can't be in the cache yet) or because the original script has changed and the cache needs refreshing.

A number of commercial and open-source bytecode caches (also called accelerators) are available on the market, such as the one contained in the Zend Performance Suite, or the open-source APC. Most often, they also modify the bytecode so as to optimize it by removing unnecessary instructions.

Bytecode caching should always be the last step in your optimization process because no matter how efficient your code is, it's always going to provide you with the same performance boost. And, as a result, it could trick you into a false sense of security that would prevent you from looking at the other performance optimization techniques available.



 
 
>>> More PHP Articles          >>> More By Sams Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: