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?
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.