Created
October 18, 2013 01:49
-
-
Save SchumacherFM/7035269 to your computer and use it in GitHub Desktop.
array_map('intval',...) vs arrayToInt()
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
<?php | |
$integers = range(100, 1000); | |
foreach ($integers as &$int) { | |
$int = (string)$int; | |
} | |
function arrayToInt(array $arr) | |
{ | |
foreach ($arr as &$a) { | |
$a = (int)$a; | |
} | |
return $arr; | |
} | |
$start = microtime(true); | |
$sum = 0; | |
for ($i = 0; $i < 100000; $i++) { | |
$converted = array_map('intval', $integers); | |
$sum += array_sum($converted); | |
} | |
var_dump($sum); | |
printf("\n%.6f msec\n", microtime(true) - $start); | |
flush(); | |
$start = microtime(true); | |
$sum = 0; | |
for ($i = 0; $i < 100000; $i++) { | |
$converted = arrayToInt($integers); | |
$sum += array_sum($converted); | |
} | |
var_dump($sum); | |
printf("\n%.6f msec\n", microtime(true) - $start); |
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
$ php -f arrayToInt.php | |
31.362757 msec | |
23.988653 msec |
array_map(fn($num) => (int)$num, ['1', '2']);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@banks A+
You said everything I was thinking as I read the original post.
You even wrote your function the way I would've.
Amen, my friend.