views:

59

answers:

4

What kind of ideas or tips and tricks do you have in order to boost PHP performance.

Something like, I use:

  $str = 'my string';
  if(isset($str[3])

Instead of:

  if(strlen($str) > 3)

Which is a bit faster.

Or storing values as keys instead of vars in array, makes searching if key exists much faster. Hence using isset($arr[$key]) instead of array_exists($arr, $key)

Shoot your ideas, i would love to hear them.

+3  A: 

Use a profiler and measure your performance.

Optimise the areas that need it.

Typical areas that will give you the most bang for effort in a typical php website.

  • think about database queries carefully. They often take up most of the execution time.
  • Don't include code you don't need
  • Don't write you own versions of the built in functions - the built in ones are compiled C and will be faster that you L33T php version.
rikh
+1  A: 

Use an OpCode cache.

Gordon
+1  A: 

Dont do as list of this things, you'll make your code unreadable... or harder to read, even by yourself. Leave this things to the Zend Engine, or to the accelerator of your choice( actually a opcode cache).

Optimizations like these may be faster now, but they may actually get slower if the guys from the zend engine starts to auto-optimize things like these.

Ex: one may speed up the strlen() function a lot by giving up on z-strings and using l-strings(the length being in the first char, or word). This in turn will end up making your (pre-optimized) script slower if you optimize like this.

Quamis
+1  A: 

Use parameterized SQL instead of mysql_query(). And reduce the overall number of database queries. Everything else are shallow optimizations.

mario