views:

179

answers:

5

How can you determine the performance consequences of your PHP code if you are not familiar with the internals? Are there ways to figure out how your code is being executed (besides simply load testing it)? I am looking for things like memory usage, execution time for algorithms.

Perhaps Joel would say, "learn C, then read the internals", but I really don't have time to learn C right now (though I'd love to, actually).

+3  A: 

Use the Xdebug extension to profile PHP code.

Jordi Bunster
+3  A: 

If you're not familiar with valgrind or similar, then to add to @Jordi Bunster's answer...

When you've had profiling on in Xdebug, you can open the dumped profile files in KCacheGrind or WinCacheGrind to get a graphical view of what is taking the time in your code.

Fortunately the xdebug documentation also explains this in detail as well as how to interpret the results: http://xdebug.org/docs/profiler

reefnet_alex
A: 

Even if you are familiar with the internals, you should still load test your assumptions. I like to use the PEAR Benchmark package to compare different code.

If you can isolate your code, you can keep your load testing simple. A typical technique is to run each option some number of times and see which one is faster. For example, if you have a class, you can write a test case and that puts it through it's paces and run it several times.

Gary Richardson
Don't get hung up on micro-optimisations like this though. Every PHP web app should be running through an opcode cache which makes micro-optimisations pretty redundant. Load testing the finished application is much better because you can then see where the time is actually going and focus on that
Flubba
I wasn't thinking so much about ' vs " type optimizations. More design and algorithm type optimizations.
Gary Richardson
A: 

You can use low-level approach such as sticking microtime() and memory_get_usage() calls into the code or you can use one of existing profiling solutions:

  1. Xdebug (free, opensource)
  2. Zend Studio/Debugger profiling (commercial)
  3. Zend Server Code Tracing (commercial)
  4. xhprof (free, opensource)

As usual, commercial tools have nice GUIs and pretty pictures, but cost money, free ones are free, but you'd probably have to invest a bit more time.

Also, PHP CGI binary has a benchmark mode with -T option, you many try running php-cgi -T 100 yourscript.php to do a poor man's benchmark.

StasM
A: 

See SD PHP Profiler for a tool that can show you graphically where your PHP applications spends its time.

Ira Baxter