3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param $x * @return int * @throws Exception */ function castToInt($x) { $result = intval($x); if ($x === $result) { return $result; } throw new \Exception("Not an int"); } /** * @param $x * @return bool */ function isInt($x) { return ($x === intval($x)); } error_reporting(-1); $time = microtime(TRUE); $even = $odd = array(); foreach (range(1, 100000) as $i) { $value = $i / 2; if (isInt($value)) { $even[] = $value; } } $isIntTime = microtime(TRUE) - $time; $time = microtime(TRUE); $even = $odd = array(); foreach (range(1, 100000) as $i) { try { $value = castToInt($i/2); $even[] = $value; } catch (\Exception $e) { //we don't care about unparseable stuff. } } $castExceptionTime = microtime(TRUE) - $time; echo 'castException microtime: '.$castExceptionTime.PHP_EOL; echo 'isInt microtime: '.$isIntTime.PHP_EOL; $ratioSlower = round($castExceptionTime / $isIntTime, 1); echo "Exception version is ".$ratioSlower." times slower.".PHP_EOL;

preferences:
54.37 ms | 1702 KiB | 5 Q