Skip to content

Instantly share code, notes, and snippets.

@tuandm
Last active March 17, 2016 03:58
Show Gist options
  • Save tuandm/5c5b8a74be65476cc4df to your computer and use it in GitHub Desktop.
Save tuandm/5c5b8a74be65476cc4df to your computer and use it in GitHub Desktop.
Casting vs intval()
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function a()
{
$a = array();
$test = @intval($a['test']);
return $test;
}
function b()
{
$a = array();
if (isset($a['test'])) {
return (int) $a['test'];
} else {
return 0;
}
}
$loop = 100000;
$time_start = microtime_float();
for ($i = 0; $i < $loop; $i++) {
a();
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'intval(): ' . $time ;
echo '<br />';
$time_start = microtime_float();
for ($i = 0; $i < $loop; $i++) {
b();
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'Casting: ' . $time;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment