Created
March 26, 2016 15:45
-
-
Save keizie/17955d0976c2f391a00c to your computer and use it in GitHub Desktop.
Running multiple generator simultaneously
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$epoch = microtime(true); | |
function times(int $a) { | |
global $epoch; | |
$i = 0; | |
while (++$i <= $a) { | |
//sleep(1); | |
var_dump(microtime(true) - $epoch); | |
yield $i.'/'.$a; | |
} | |
} | |
$aAndB = new AppendIterator(); | |
$aAndB->append(times(10)); | |
$aAndB->append(times(5)); | |
$both = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); | |
$both->attachIterator(times(10)); | |
$both->attachIterator(times(5)); | |
foreach ($both as list($i,$j)) { | |
sleep(1); | |
var_dump($i,$j); | |
} | |
/* | |
float(1.8119812011719E-5) | |
float(4.7922134399414E-5) | |
float(5.1975250244141E-5) | |
string(4) "1/10" | |
string(3) "1/5" | |
float(1.0001580715179) | |
float(1.000167131424) | |
string(4) "2/10" | |
string(3) "2/5" | |
float(2.0002980232239) | |
float(2.0003049373627) | |
string(4) "3/10" | |
string(3) "3/5" | |
float(3.0004329681396) | |
float(3.0004439353943) | |
string(4) "4/10" | |
string(3) "4/5" | |
float(4.0005650520325) | |
float(4.0005760192871) | |
string(4) "5/10" | |
string(3) "5/5" | |
float(5.0006899833679) | |
string(4) "6/10" | |
NULL | |
float(6.0007889270782) | |
string(4) "7/10" | |
NULL | |
float(7.0009140968323) | |
string(4) "8/10" | |
NULL | |
float(8.0010290145874) | |
string(4) "9/10" | |
NULL | |
float(9.0011451244354) | |
string(5) "10/10" | |
NULL | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment