Do we still need intdiv()
since PHP 8.0 /
operator supports error throwing when dividing by 0?
intdiv(10, 3) == 3;
intdiv(-10, 3) == -3;
(int)(10 / 3) == 3;
(int)(-10 / 3) == -3;
floor(10/3) == 3;
floor(-10/3) == -4;
<?php
$start1 = microtime(true);
$i = 1;
while ($i < 1000000) {
(int)(10 / $i);
$i++;
}
$time_elapsed_secs1 = microtime(true) - $start1;
$start2 = microtime(true);
$i = 1;
while ($i < 1000000) {
intdiv(10, $i);
$i++;
}
$time_elapsed_secs2 = microtime(true) - $start2;
$result1 = (1 - $time_elapsed_secs2 / $time_elapsed_secs1) * 100;
($time_elapsed_secs1 > $time_elapsed_secs2) ? $text1 = "slower" : $text1 = "<<< FASTER";
echo "1 time:". $time_elapsed_secs1 . " => " . abs(round($result1, 2)) ."% $text1 - (int) /" . PHP_EOL;
$result2 = (1 - $time_elapsed_secs1 / $time_elapsed_secs2) * 100;
($time_elapsed_secs2 > $time_elapsed_secs1) ? $text2 = "slower" : $text2 = "<<< FASTER";
echo "2 time:". $time_elapsed_secs2 . " => " . abs(round($result2, 2)) ."% $text2 - intdiv()" . PHP_EOL;