How can we get the execution time of a script?

Asked

Viewed 1,014 times

5

2 answers

5


The function microtime() returns the timestamp current. You can use it to "mark" the initial time and the end of your script in order to obtain the execution time.

Follow an example:

<?php
// Armazena o timestamp antes da execucao do script
$tm_inicio = microtime( true ); 

//
// Codigo Script 
//

// Armazena  o timestamp apos a execucao do script
$tm_fim = microtime( true );

// Calcula o tempo de execucao do script 
$tempo_execucao = $tm_fim - $tm_inicio;

// Exibe o tempo de execucao do script em segundos
echo '<b>Tempo de Execucao:</b> '.$tempo_execucao.' Segs';

?>

Reference:

http://php.net/manual/en/function.microtime.php

I hope I helped!

0

In the example of the other answer I used this:

class Bench
{

    protected static $ini;

    public static function Start($id = 0)
    {
        /*
        Get the start time in microseconds, as a float value
        */
        self::$ini[$id] = microtime(true);
    }

    public static function Calc($id = 0)
    {
        /*
        Get the difference between the start and the end time in microseconds, as a float value
        */
        $end = microtime(true);
        $diff = $end - self::$ini[$id];

        /*
        Break the difference into seconds and microseconds
        */
        return array($diff, $diff - intval($diff), self::$ini[$id], $end);
    }

}

Mode of use

// Inicia a contagem. Após essa linha não pode ter nada que não seja relacionado ao que deseja testar.
Bench::Start();

// aqui o bloco de script que deseja testar

// Finaliza a contagem e retorna o resultado.
$bench = Bench::Calc();
echo '<pre>';
print_r($bench);
echo '</pre>';

Important to note that you got an unexpected result due to this method return Techo Calc()

return array($diff - intval($diff), self::$ini[$id], $end);

This is the original return because I usually do tests that return less than 1 second. usually 1000 to 100,000 iterations. So I don’t need to know number of seconds.

For your case, where you applied 1 million iterations, return this way:

return array(
    $diff, // literal
    $diff - intval($diff), // parte decimal
    self::$ini[$id], // microtime inicial
    $end // microtime final
);

In the first key, the value in seconds.

Note: Be aware that they are tests within the context of the original question: Compared to Datetime class, the date function is more performative?

  • 1

    Daniel, but then if I use this new Return also works for 100 thousand?

  • I tried it now with 100 thousand and with this new Return, and it gives 2.5 seconds, I assume, since the script takes 2 seconds to appear the result.

  • As I mentioned, I usually do a test that is not more than 1 second. so I ignore the value of $diff. In your case, as it is a lower configuration and is passing 1 second, it is better to return the value of $diff.

  • out of curiosity, if possible, test with PHP7.

  • Daniel I do not know if it is the OS or PHP, but now in Windows10 with PHP7 already gave values similar to yours.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.