Created
December 24, 2014 10:18
-
-
Save hehachris/4c2d3078c4dc24172682 to your computer and use it in GitHub Desktop.
PHP 5.5+: Array vs Generator
This file contains 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 | |
function xrange($start, $end) | |
{ | |
$array = []; | |
for ($i = $start; $i <= $end; $i++) { | |
$array[] = $i; | |
} | |
return $array; | |
} | |
$max = isset($argv[1]) ? (int) $argv[1] : 1000000; | |
$items = xrange(1, $max); | |
echo "1: " . memory_get_usage() . "\n"; | |
foreach ($items as $val) { | |
} | |
echo "2: " . memory_get_usage() . "\n"; |
This file contains 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 | |
function xrange($start, $end) | |
{ | |
for ($i = $start; $i <= $end; $i++) { | |
yield $i; | |
} | |
} | |
$max = isset($argv[1]) ? (int) $argv[1] : 1000000; | |
$items = xrange(1, $max); | |
echo "1: " . memory_get_usage() . "\n"; | |
foreach ($items as $val) { | |
} | |
echo "2: " . memory_get_usage() . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment