Tracking a Stack of Function Calls with the Xdebug Extension - Review: the xdebug_time_index() function (
Page 2 of 4 )
First, I'd like to quickly review the practical example developed in the preceding article of this series, in case you haven't had a chance to read that article. The example was aimed at demonstrating how to use the “xdebug_time_index()” function for building a pair of timing scripts.
That being said, here are the two code samples that create those timing scripts. Have a look at them, please:
(example on using the 'xdebug_time_index()' function within a procedural script)
echo 'Starting time : '.xdebug_time_index().'<br />';
for($i=0;$i<=3;$i++){
// do nothing
sleep(1);
}
echo 'Total script execution time : '.xdebug_time_index();
/* displays the following
Starting time : 0.001168966293335
Total script execution time : 3.9993019104004
*/
(example on using the 'xdebug_time_index()' function within an object-based script)
class Timer{
public function __construct(){}
public function getTime(){
return xdebug_time_index();
}
}
$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
*/
As you can see, the examples listed above illustrate how to use the “xdebug_time_index()” function to build a few primitive timing applications. In the first case, the function is utilized in the context of a procedural script, while the second example is a bit more interesting, since it shows how to create a basic timer class by using the function. Quite simple to grasp, right?
Hopefully, at this point you already realized how useful the “xdebug_time_index()” function can be when it comes to calculating the execution time of a certain PHP application. So what's next? Well, as I said in the introduction, the Xdebug extension includes yet another handy function, named “xdebug_get_function_stack(),” that permits us to explore with relative ease the stack of function calls generated when a script is parsed by the PHP engine.
Therefore, in the next section I’ll be explaining how to use this function in a concrete situation. In fact, I'll use a hands-on example to demonstrate it explicitly. This way, you can get a better idea of its actual functionality.
To learn how this brand new example will be developed, go ahead and read the next segment. It’s only one click away.