1

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.

aviv
  • 2,719
  • 7
  • 35
  • 48
  • 1
    *related* : [List of Big-O for PHP functions](http://stackoverflow.com/questions/2473989/list-of-big-o-for-php-functions) – Felix Kling Jun 29 '10 at 11:26
  • 8
    Those are micro-optimizations, they do not gain you anything unless you really, _really, **really** removed all other bottlenecks (and then you wouldn't use PHP anyways, i guess). Write readable code and don't even think about things like setting variables to 0 by writing `$foo ^= $foo;` –  Jun 29 '10 at 11:26
  • Those are my ideas. Using opcode cache is a good answer. Can you give more ideas? – aviv Jun 29 '10 at 13:01

4 Answers4

3

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.
Rik Heywood
  • 13,816
  • 9
  • 61
  • 81
2

Use an OpCode cache.

Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request (some or all of which may never even be executed). To further improve performance, the cached code is stored in shared memory and directly executed from there, minimizing the amount of slow disk reads and memory copying at runtime.

Gordon
  • 312,688
  • 75
  • 539
  • 559
1

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
  • 10,924
  • 12
  • 50
  • 66
1

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

mario
  • 144,265
  • 20
  • 237
  • 291