PHP5.5 detailed examples of the usage of the iteration generator, php5.5 Generator
This example describes the usage of the PHP5.5 iteration generator. We will share this with you for your reference. The details are as follows:
PHP5.5 introduced the concept of iteration generator. The concept of iteration has long been available in PHP, but the iteration generator is a new feature of PHP, which is similar to the iteration generator in python3, let's see how the PHP5.5 iteration generator is defined.
<?phpfunction xrange($start, $end, $step = 1) { for ($i = $start; $i <= $end; $i += $step) { yield $i; }}foreach (xrange(1, 1000000) as $num) { echo $num, "\n";}
Note: yield builds an iterator Based on The yeild keyword. The xrange function differs from the previous function. Generally, a return value is used, and a yield value indicates that this is an iterator. This iterator generates this value once every loop, so it is called an iteration generator, the iteration generator function can perform a foreach loop and generate a value each time.
Before PHP5.5, The Iterator interface was implemented by defining classes to construct the Iterator. Constructing the Iterator through yield will improve performance and save system overhead.
The advantage of this method is obvious. It allows you to process big data sets without loading them into the memory at once, or even process infinite data streams.
As shown in the preceding example, the iterator function is to generate a number from 1 to 1000000 and output it cyclically. The previous method is to generate a number from 1 to 1000000 to the array, it will occupy a lot of memory, because it is necessary to generate all the results in advance, instead of generating as needed, that is, when calling the xrange iterator, the functions are not actually running until each iteration.
Let's take a look at the PHP official website example:
<?phpfunction xrange($start, $limit, $step = 1) { for ($i = $start; $i <= $limit; $i += $step) { yield $i; }}echo 'Single digit odd numbers: ';/* * Note that an array is never created or returned, * which saves memory. */foreach (xrange(1, 9, 2) as $number) { echo "$number ";}echo "\n";?>
Here, xrange is an iteration with the same functions as range. If the range function is used, the internal implementation of the function will store the intermediate process of each iteration, that is to say, every intermediate variable has a memory space. First, the memory space used by the program will be large, and memory allocation and memory recovery will lead to a longer running time of the program. However, if you use the xrange function implemented by yield, all the intermediate variables in it only use one memory $ I, which reduces the time and space required.
Why is yield so effective? I think of yield in lua, which is the concept of coroutine. In lua, when the program runs to yield, the coroutine is used to record the Context Environment and return the program operation permission to the main function. When the main function calls resume, the coroutine is refreshed to read the context of the yield Record. This forms a multi-coroutine operation at the language level. The same applies to yield in php 5.5. When the program runs to yield, the current program will invoke the context of the coroutine record, and then the main function will continue to operate, in php, the keyword like resume is not used, but the coroutine called during use. For example, the foreach iterator in the previous example can invoke yield. So the above example can be understood.