A simple static class to dissect execution times of a full script or parts of it.
Include Timing.php
via include
, require
or composer autoload
and use it as a static class.
Calculate the execution time between two points.
Timing::start('random calc');
for ($i = 0; $i < 10000; $i++) {
$r = rand();
}
Timing::stop('random calc');
Saves the time elapsed since the script started to the break point
$var = rand();
sleep(2);
Timing::break();
$var = rand();
Calculate the execution time of a function
$returnVal = 0;
for ($i = 1; $i < 3; $i++) {
$returnVal += Timing::measure('someFunction('.$i.')', function() use ($i) {
sleep($i);
return $i;
});
}
var_dump($returnVal);
Adds a function called every time a new timing has been saved
Timing::addCallback(function($key, $time) {
var_dump("Collected timing for: $key: ". round($time,4));
});
Returns all saved times under a key=>value array
Array
(
[start] => 0
[random calc] => 0.0003
[someFunction(1)] => 1.0001
[break @ sample.php:45] => 1.0006
[someFunction(2)] => 2.0001
)