Created
June 4, 2013 01:17
-
-
Save BlakeGardner/5702886 to your computer and use it in GitHub Desktop.
Simple implementation of range as a generator
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 | |
$data = range(0, 1000000); | |
echo sprintf('%02.2f', (memory_get_usage() / 1048576))." MB of memory used\n"; | |
// output: 137.92 MB of memory used | |
foreach ($data as $key => $val) { | |
//echo "key: ".$key." value: ".$val."\n"; | |
} |
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 | |
/** | |
* Simple implementation of range() as a generator | |
* @param int $start | |
* @param int $end | |
* @return void | |
*/ | |
function range_yield($start, $end) { | |
for ($index = $start; $index <= $end; $index++) { | |
yield $index => $index; | |
} | |
} | |
$data = range_yield(0, 1000000); | |
echo sprintf('%02.2f', (memory_get_usage() / 1048576))." MB of memory used\n"; | |
// output: 0.22 MB of memory used | |
foreach ($data as $key => $val) { | |
//echo "key: ".$key." value: ".$val."\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment