note 124365 added to language.generators.comparison
| From: | jorgeley at gmail dot com | Date: | Mon, 04 Nov 2019 09:44:28 +0000 |
| Subject: | note 124365 added to language.generators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212887@lists.php.net to get a copy of this message | ||
I think the power of generators is underestimated here, look at my example:
<?php
/**
* simple example class just to have something to instantiate
*/
class obj {
private $i = 1;
private $a = [];
function __construct($i = 1) {
$this->i = $i;
$this->a = range(0, $i);
}
public function getI() {
return $this->i;
}
//more getters and setters...
}
/**
* this is a common way of returning objects in a bulk
* @param int $n
* @return \obj
*/
function returnObjects($n = 1000) {
$objs = [];
for ($i = 1; $i <= $n; $i++) {
$objs[] = new obj($i);
}
return $objs;
}
/**
* this is a better way using generator, rather than returning all objects,
* it returns one by one (it saves the state of the function in every call)
* @param int $n
*/
function generateObjects($n = 1000) {
for ($i = 1; $i <= $n; $i++) {
/**
* 'yield' returns the object and save the status of the function, so
* next call starts from next loop iteration and so on...
*/
yield (new obj($i));
}
}
//main script: get current memory, run one of the functions and calculate memory usage after
$m = memory_get_peak_usage();
/**
* comment 'returnObjects()' call bellow and uncomment 'generateObjects()' call
* if you want to see the generator memory usage
*/
//$objs = returnObjects();
/**
* comment 'generateObjects()' and uncomment 'returnObjects()' call if you
* want to see the common function return memory usage
*/
$objs = generateObjects();
foreach ($objs as $obj) {
echo get_class($obj) . ": {$obj->getI()}\n";
}
echo "total memory comsuption: " . (memory_get_peak_usage() - $m) . " bytes\n";
?>
what is the outcome? Using the 'returnObjects()' we return an array within 1000 objects,
but using the 'generateObjects()' we only instantiate one object since the yield returns
(stop the loop) but also saves the state of the function, so next call the function resumes rather
than restarting. In my environment I got a difference of 37K to 25M
Thanks to my dear friend Ivan Frezza who helped me to understand this better!
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.168 (proxied: 93.107.43.213)
----
Manual Page -- http://php.net/manual/en/language.generators.comparison.php
Edit -- https://master.php.net/note/edit/124365
Del: integrated -- https://master.php.net/note/delete/124365/integrated
Del: useless -- https://master.php.net/note/delete/124365/useless
Del: bad code -- https://master.php.net/note/delete/124365/bad+code
Del: spam -- https://master.php.net/note/delete/124365/spam
Del: non-english -- https://master.php.net/note/delete/124365/non-english
Del: in docs -- https://master.php.net/note/delete/124365/in+docs
Del: other reasons-- https://master.php.net/note/delete/124365
Reject -- https://master.php.net/note/reject/124365
Search -- https://master.php.net/manage/user-notes.php