Simple analysis of PHP generator Generators

不言
Release: 2023-04-03 15:14:02
Original
1634 people have browsed it

This article introduces to you a simple analysis of PHP generators. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What are Generators

Generators allow you to write code in a foreach block to iterate over a set of data without creating an array in memory, that It will hit your memory limit or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and instead of a normal function returning only once, the generator can yield as many times as needed to generate the values that need to be iterated over.

A simple example is to use a generator to reimplement the range() function. The standard range() function needs to generate an array in memory containing every value within its range, and then return the array, resulting in multiple large arrays. For example, calling range(0, 10000) will cause the memory usage to exceed 4 MB.

Example code:

= 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("内存消耗量%s\n", convert($end-$start)); function convert($size){ $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; }
Copy after login

Environment

  • PHP7.1

  • Xdebug

Execute command

  • php test.php The result is returned: memory consumption 416 b

  • php test.php range Result return: memory consumption 4 mb

Test result discussion

  • Through the execution of the code, compare the similarities and differences between the PHP standard function rang() and the custom function rangeWithGenerators(). Achieving the same purpose but consuming different memory, the advantages of the generator are obvious.

  • rangeWithGenerators() uses a generator, and the key to the generator is to use the yield keyword. The common use of yield involves two process control statements, foreach and for. The yield variable $i in the for loop in rangeWithGenerators() (yield can be understood as generating a value, just like return can be understood as returning a value).

  • rangeWithGenerators() function returns a generator Generators (use var_dump($iterate) to view its type).

  • Since the rangeWithGenerators() function returns a generator, we can use it in the following two ways:

  1. foreach loops the Generators. In this example, this method is used to read the Generators.

  2. PHP standard function iterator_to_array(), this function converts Generators into an Array.

Recommended related articles:

Introduction to the use of json_encode in PHP

How to use curl to receive POST in php The address is the method to create an interface

The above is the detailed content of Simple analysis of PHP generator Generators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!