Contact Us

Using Profilers

Posted on 21/3/07 by Tim Koschützki

What is a profiler?

A profiler is an application that inspects your php code. It determines, how often every function (including the built-in functions) have been called and how much time they needed to complete. This is really great for optimising your program and makes finding the really tough and slow functions and methods in your application very easy.

Should I use a profiler?

Of course, every serious programmer should use a profiler to debug his code. Making a speedy application is one of the fundamentials for its success. Professional studies state, that the average user waits around six to seven seconds for an application or website to load - before he moves on. For you that would mean lost business, lost money. You sure don't want to lose money due to a call to preg_replace with the wrong regular expression.

What profiler should I use?

If you ask me, there is one great profiler, which is used by thousands of php programmers worldwide. It's php's XDebug Extension. It is under heavy development and has a great support system, installation guidelines and general documentation.

Here is an example code snippet on how you can use XDebug:

// Check whether xdebug is installed
if(!extension_loaded('xdebug')) die('Xdebug is required!');

// Start the profiling

class MyClass {
   static function hello_world() {
        // echo 'Hello World'

// Let's use xdebug to profile how much time calling a static class method using call_user_func() and MyClass::here2 takes

$cycles = 50000;

for($i=0;$i<$cycles;$i++) MyClass::here2();

for($i=0;$i<$cycles;$i++) call_user_func(array('MyClass','here2'));

// Display the xdebug report

The code should be pretty straightforward. We simply use two different methods of calling the same static function of the class MyClass2. Once we call it 50000 times using MyClass::here2 and once using call_user_func().

If you have correctly installed xdebug, you will find out that the call_user_func actually takes about 3 times as much time as MyClass::here2().

Using XDEBUG_PROFILER_NC in our call to xdebug_dump_function_profile tells xdebug to merge all function calls in the same line into a single entry in the report. There are several other constants, i.e. methods of profiling available. Those range from sorting function names in order of their speed to reporting the average execution time for each function.

Two versions of xdebug

You have to know that there are now two versions of Xdebug available. One will just work the way my code documents it. This is called XDebug1 in the xdebug documentation. However, an even better coding practice is to use XDebug2, which dumps its reports to files which can then be used via Wincachegrind.


Using xdebug and profiling in general is a quick way of defining the slow parts of your application, without having to code a thousand calls to array_sum(explode(' ',microtime()));.

Every programmer should use profiling. It's a so-called best coding practice. Although the example provided may seem trivial, XDebug is very powerful when you profile your entire application.


You can skip to the end and add a comment.

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.