This article introduces to you the content of PHP generator generators Simple analysis, there is a certain reference value, the need for friends can refer to, I hope to help you.
What is generator generators
The generator allows you to write code in a foreach code block to iterate over a set of data without having to create an array in memory, which will limit your memory to a maximum or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and the normal function returns only once differently, and the generator can yield multiple times as needed to generate values that need to be iterated.
A simple example is to use the generator to re-implement the range () function. The standard range () function needs to generate an array in memory that contains every value within its scope, and then returns the array, resulting in multiple large arrays. For example, calling range (0, 10000) will result in memory consumption exceeding 4 MB.
Example code:
<?php/** * Created by Phpstorm. * user:chenbotome@163.com * DATE:2018/7/30 * Time: Morning 11:29 * * $start = Xdebug_memory_usage (); $num = 10000;function RangeW Ithgenerators ($start, $limit, $step = 1) {if ($start < $limit) {if ($step <= 0) {throw new L Ogicexception (' Step must be +ve '); } for ($i = $start; $i <= $limit; $i + = $step) {yield $i; }} else {if ($step >= 0) {throw new Logicexception (' Step must be-ve '); } for ($i = $start; $i >= $limit; $i + = $step) {yield $i; }}}if ($ARGC = = = 1) {$iterate = rangewithgenerators (0, $num, 1); foreach ($iterate as $value) {echo $value. "\ n"; }//var_dump (Iterator_to_array ($iterate));} if ($ARGC = = = 2) {$test = range (0, $num, 1); foreach ($test as $value) {echo $value. "\ n"; }} $end = Xdebug_memory_usage (), Echo sprintf ("Memory Consumption%s\n", convert ($end-$start)), function convert ($size) { $unit =array (' B ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB '); Return @round ($size/pow (1024x768, ($i =floor (log ($size, 1024))), 2). ' '. $unit [$i];}
Environment
Execute command
Test Result Discussion
By executing the code, we compare the similarities and differences between PHP standard function rang () and the Custom Function rangewithgenerators (). Achieve the same purpose, but the use of different memory, the advantages of the generator is obvious.
Rangewithgenerators () uses the generator, and the key to the generator is to use the yield keyword, and the yield common use involves a foreach and for two process control statements. Rangewithgenerators () in the For Loop yield variable $i (yield can be understood as a build-value, as return can be interpreted as a return-value).
The Rangewithgenerators () function returns a generator generators (using Var_dump ($iterate) to view its type).
Since the rangewithgenerators () function returns a generator, we can use the following two ways:
Foreach Loop the generators, in this case, use this method to read generators. The
-
PHP standard function Iterator_to_array (), which converts generators to an array.