How to calculate runtime in PHP


I would like to know how long it takes for a particular function, loop loop, etc… to run. I resorted to the microtime() function, but I get a little lost to know exactly how much time was spent. And my questions are as follows:

1- How can I convert microseconds into seconds? So that it's a little legible and you get a sense of how much time was spent during execution.


   $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- There is some tool or resource available for PHP that can check in real time something like the execution speed of the script, of a function, loop loop, etc.


Microsecond (mS) is a multiple of the second, a unit of time, prefixed by the base standards micro multiplier (µ), equal to 0.000001 second(1 microsecond = 1.0E-6 seconds). So to get the time in seconds, you should divide by 1000000.

Starting with PHP 5 you can use microtime at the beginning and end of your code:

    $inicio = microtime(true);
    $fim = microtime(true);
    $tempo = $fim - $inicio;
    printf("Processado em: %0.16f segundos", $tempo/1000000);

See it working:

As of PHP 5.4.0 , there is no need to start the start time, the $_SERVER["REQUEST_TIME_FLOAT"] already has it:

    $tempo = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
    printf("Processado em: %0.16f segundos", $tempo/1000000);

See Working:


Microseconds to Seconds Conversion Calculator

PHP microtime documentation

How to find php runtime?

Scroll to Top