5
You’re reading the question here Compared to Datetime class, the date function is more performative? and did some tests with the answer by Daniel Omine, but I got results that didn’t seem at all reliable.
How can I get the runtime of a PHP script?
5
You’re reading the question here Compared to Datetime class, the date function is more performative? and did some tests with the answer by Daniel Omine, but I got results that didn’t seem at all reliable.
How can I get the runtime of a PHP script?
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?
Browser other questions tagged php performance
You are not signed in. Login or sign up in order to post.
Daniel, but then if I use this new Return also works for 100 thousand?
– Jorge B.
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.
– Jorge B.
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.
– Daniel Omine
out of curiosity, if possible, test with PHP7.
– Daniel Omine
Daniel I do not know if it is the OS or PHP, but now in Windows10 with PHP7 already gave values similar to yours.
– Jorge B.