PHP Generator --- yield iterator

1, the iterative generator
core generator is a yield keyword, a generator function that looks like an ordinary function, except that: ordinary function returns a value, and a generator can yield it needs to generate a lot of value . Generating function is called, it returns an object that can be traversed.
yield and return somewhat similar, but the difference is, return value and return code execution is halted, and the return code will yield a value to the cycle call this generator and only suspend the generator function.

function gen_one_tow_three () {
     for ( $ i =. 1; $ i <=. 3; $ i ++ ) {
         // Note: the value of the variable $ i between the different transfer yield is maintained. 
        the yield $ I ; 
    } 
} 
$ Generator = gen_one_tow_three ();
 var_dump ( $ Generator );
 echo "<br>" ;
 var_dump ( $ Generator the instanceof the Iterator);
 echo "<br>" ;
 the foreach ( $ Generator  AS  $ value ) {
     echo  $ value."\n";
}        

Output:
Object (Generator). 1 # (0)} {
BOOL (to true)
. 1 2. 3
calls gen_one_tow_three () when inside the code does not really performed, but instead returns a generator object $ generator = Generator Object () , $ generator instanceof Interator Description Generator matters Iterator interface, can be traversed using foreach, each iteration will implicitly call the current (), next (), key (), valid () methods. (Method Generator class)
2, large data
following xrange by implementing a function will be briefly described:

function xrange($start,$end,$step = 1){
    for ($i=$start; $i <=$end; $i+=$step) { 
        yield $i;
    }
}

foreach(xrange(1,1000000) as $num){
    echo $num,"\n";
}

The above xrange () function and provides built php range () function the same function, except that range () function returns a value from the array containing 1 to 100w, and xrange () function returns these in turn output worth an iterator, without actually return an array.
The advantage of this approach is obvious, it can let you in dealing with large data sets do not have disposable loaded into memory. Even you can handle infinite data stream
3, large files
in PHP to read large files, it often happens out of memory, if the file is too large, it can not read a play, using the yield to achieve large files It reads.
3.1) the old-fashioned reading

function readLocalFile($fileName){
    $handle = fopen($fileName,'r');
    $lines = [];
    while(!feof($handle)){
        $lines[] = fgets($handle);
    }
    fclose($handle);
    return $lines;
}


3.2) yield read mode

function readYieldFile($fileName){
    $handle = fopen($fileName,'r');
    while(!feof($handle)){
        yield fgets($handle);
    }
    fclose($handle);
}

// In order to facilitate testing, a read write memory helper function

function formatBytes($bytes){
    if($bytes<1024){
        return $bytes . "b";
    }else if($bytes <1048576){
        return round($bytes / 1024,2) ."kb";
    }
    return round($bytes / 1048576,2) . "mb";
}
// first, all.zip 158M is a compressed file 
readLocalFile ( './ all.zip' );
 echo formatBytes (memory_get_peak_usage ());

报错:Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 123 bytes) in

//第二种
$lines = readYieldFile('./all.zip');
foreach($lines as $row){}
echo formatBytes(memory_get_peak_usage()); 


Output: 141.3kb

Summary:
old-fashioned reading, returns an array that contains each row of data, while the yield of the way matters iterator returned, but will not return to the real array.
This method can make you a bit when dealing with large data sets do not have disposable loaded into memory, and even can handle infinite data streams.

 

Guess you like

Origin www.cnblogs.com/lxhyty/p/11288040.html