Last active
March 17, 2016 03:58
-
-
Save tuandm/5c5b8a74be65476cc4df to your computer and use it in GitHub Desktop.
Casting vs intval()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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