3
I would like to know how long it takes for a certain function, loop loop, etc... to be executed. I use the function microtime()
, but I am a little lost to know exactly what time was spent. And my doubts are as follows:
1- How can I convert microseconds into seconds? So that it becomes somewhat readable and if you have a sense of the time that was spent during execution.
<?php
$tempoInicial = round(microtime(true) * 1000);
$var = 100000;
for ($i=0; $i < $var; $i++) {
echo "$i -> ";
}
$tempoFinal = round(microtime(true) * 1000);
$total = $tempoFinal - $tempoInicial;
echo "Resultado: Tempo inicial: $tempoInicial <br> Tempo final:
$tempoFinal <br> Total: $total";
2- Is there any tool or resource available for PHP that can check in real time something like the execution speed of the script, of a function, loop, etc.
microtime(true)
returns something like1572433011.7642
(number of seconds plus fractions of seconds). By multiplying by 1000 and rounding, you get the amount of milliseconds (in case,1572433011764
), then to get in seconds, just divide by 1000. That’s it?– hkotsubo