In the section that you just read, I explained how to use the “xdebug_time_index()” function, to create a simple procedural timing script. However, it’s also feasible to take advantage of the intrinsic capabilities of this function to build a timer class. Want to see how this class can be built in a few easy steps? Examine the following code sample, which defines a primitive “Timer” class that obviously uses the “xdebug_time_index()” function as its workhorse. Here it is: class Timer{ public function __construct(){} public function getTime(){ return xdebug_time_index(); } } Indeed, the above “Timer” class doesn’t bear any further discussion. It only implements a single method called “getTime(),” which behaves as a wrapper for the “xdebug_time_index()” function. That’s all. It would, however, be pretty interesting to see how this class can be used for timing a simple application. Thus, below I coded a final hands-on example that utilizes the previous timing class for calculating the total execution time of a basic script. Here it is: $timer=new Timer(); echo 'Starting time :'.$timer->getTime().'<br />'; for($i=0;$i<=5;$i++){ sleep(1); } echo 'Total script execution time :'.$timer->getTime(); /* displays the following Starting time :0.0012168884277344 Total script execution time :5.9927868843079 */ Here you have it. Now that you’ve seen for yourself how simple it is to build a timer class using the “xdebug_time_index()” function, hopefully this will fire up your own creativity, and you’ll start building your own benchmarking scripts. Final thoughts In this sixth episode of the series, I walked you through using the “xdebug_time_index()” function that comes with the X-debug extension, to perform basic benchmarking on several PHP scripts. Undeniably, the utilization of this function is pretty intuitive, so in theory you shouldn’t have major problems using it for timing your own PHP programs. However, this series still has a final installment, in which you’ll learn how to employ another handy function of the X-debug library. It's called “xdebug_get_function_stack(),” and it will be utilized for exploring the stack of function calls generated by the PHP engine during the execution of a script. Now that you’ve been warned about the subject of the final article of this series, you won’t want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|