I'm trying to profile some piece of php code by computing the time in microseconds it takes it to execute. I have created a profiler function that take another function as parameter like this
function profiler($funct)
{
$raw_start_time = microtime();
$funct();
$raw_end_time = microtime();
$start_time = 1000000 * (double) explode(" ", $raw_start_time)[0];
$end_time = 1000000 * (double) explode(" ", $raw_end_time)[0];
return $end_time - $start_time;
}
I use my profiler()
function like this
$duration = profiler(function() {
//code i want to profile
});
echo $duration;
The function runs correctly most times and returns the correct time duration of execution. However, I get negative values in some test cases. What could be the problem?