HomePHP Page 3 - Benchmarking with the Xdebug Extension
Benchmarking PHP applications with the X-debug extension - PHP
Welcome to the sixth installment of a series that covers debugging in PHP with the Xdebug extension. Comprised of seven approachable tutorials, this series gets you started using the set of functions provided by this helpful library, so you can begin debugging your own PHP applications with an excellent level of control.
As you may guess, any decent PHP debugging library must have the capability of benchmarking programs with relative ease, and certainly Xdebug isn’t an exception. The library in question comes equipped with a handy function called “xdebug_time_index(),” which can be used for timing PHP scripts with extreme simplicity.
To demonstrate this function, I’m going to start coding a basic example, which will use it within a typical “for” loop. Here’s the corresponding code sample; have a look at it, please:
echo 'Starting time : '.xdebug_time_index().'<br />';
// do nothing
echo 'Total script execution time : '.xdebug_time_index();
/* displays the following
Starting time : 0.001168966293335
Total script execution time : 3.9993019104004
If you analyze the above code sample, you’ll realize how simple it is to benchmark a specified PHP script using the aforementioned “xdebug_time_index()” function. As shown above, the function has been called twice, that is before the “for” statement starts its iteration, and at the end of the script, to display its total execution time.
Of course, if you’re anything like me, then you may want to see how the flexibility offered by the “xdebug_time_index()” function can be exploited to create an object-oriented timing application with extreme ease.
With that idea in mind, the last section of this article will be focused on explaining how to build a basic timer class, which will use internally the previous “xdebug_time_index()” function for benchmarking PHP scripts.
This brand new timer class will be developed in the following segment, so jump ahead and read the new few lines. I’ll be there, waiting for you.