Simple parsing of PHP generator generators

Source: Internet
Author: User
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

    • PHP7.1

    • Xdebug

Execute command

    • PHP test.php results returned: memory consumption 416 b

    • PHP test.php Range result return: Memory consumption 4 MB

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

    1. PHP standard function Iterator_to_array (), which converts generators to an array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.