Skip to content

Instantly share code, notes, and snippets.

@BlakeGardner
Created June 4, 2013 01:17
Show Gist options
  • Save BlakeGardner/5702886 to your computer and use it in GitHub Desktop.
Save BlakeGardner/5702886 to your computer and use it in GitHub Desktop.
Simple implementation of range as a generator
<?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";
}
<?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