ΪʲôҪʹÓà SPLÖÐµÄ SplQueueʵÏÖ¶ÓÁÐ

2019-05-16 23:58:40À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

½ñÌì¿´phpµÄSPL±ê×¼¿â²¿·ÖÀïÃæÉæ¼°µ½Êý¾Ý½á¹¹ÆäÖÐÓÐ SplQueue À´ÊµÏÖ¶ÓÁÐЧ¹û£¬µ«ÊÇÎÒ¸Õ½Ó´¥phpµÄʱºòѧϰµ½µÄÊÇ Ê¹ÓÃarrayµÄ array_push ºÍ array_pop ¾Í¿ÉÒÔʵÏÖ¶ÓÁÐЧ¹ûÀ²°¡£¬ÄÇô˵ SPLÖеÄÕâ¸öÊDz»ÊÇÏԵúܼ¦ÀßÄØ£¿£¿ºóÀ´½ø¹ý²éѯ×ÊÁϲ¢ÇÒʵ¼ÊÔËÐÐһϳÌÐòºó·¢ÏÖ£¬ÆäʵÔÚÐÔÄÜ·½Ã棬ʹÓÃsplµÄÊý¾Ý½á¹¹±ÈʹÓÃarrayÄ£Äâ³öÀ´µÄ¶ÓÁÐÔÚÐÔÄÜÉÏÇ¿ºÜ¶à£º

  arrayÊý×éÄ£Äâ¶ÓÁУ¬´¦Àí100000ÈÎÎñ

list($t1, $t2) = explode(' ', microtime());
$st = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
$arrq = array();
for($i = 0; $i <100000; $i++)
{
    $data = "hello $i\n";
    array_push($arrq, $data);
    if ($i % 100 == 99 and count($arrq) > 100)
    {
        $popN = rand(10, 99);
        for ($j = 0; $j < $popN; $j++)
        {
            array_shift($arrq);
        }
    }
}
$popN = count($arrq);
for ($j = 0; $j < $popN; $j++)
{
    array_shift($arrq);
}

list($t1, $t2) = explode(' ', microtime());
$et = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);

echo $et - $st;

Ö´ÐÐÈý´Îȡƽ¾ùֵΪ£º3900 ms

  ʹÓÃSplQueue

list($t1, $t2) = explode(' ', microtime());
$st = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);

$splq = new SplQueue;
for($i = 0; $i < 100000; $i++)
{
    $data = "hello $i\n";
    $splq->push($data);

    if ($i % 100 == 99 and count($splq) > 100)
    {
        $popN = rand(10, 99);
        for ($j = 0; $j < $popN; $j++)
        {
            $splq->shift();
        }
    }
}
list($t1, $t2) = explode(' ', microtime());
$et = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);

echo $et - $st;

Ö´ÐÐÈý´Îȡƽ¾ùֵΪ£º117 ms

ÐÔÄÜÌáÉý 33 ±¶

¹ØÓÚspl±ê×¼¿âÕâ¿é£¬¿´À´»¹Òª¶àÑо¿Ñо¿£¬¼ÈÈ»ÎĵµÖÐÓÐÕâ¸ö¿â£¬ÄÇô¿Ï¶¨ÓÐËü¶ÀÌصĵط½£¡

²Î¿¼Îĵµ £ºhttps://wiki.swoole.com/wiki/page/507.html

 


Ô­ÎÄÁ´½Ó:https://www.cnblogs.com/itsuibi/p/10870608.html
ÈçÓÐÒÉÎÊÇëÓëÔ­×÷ÕßÁªÏµ

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£ºPHP ³£ÓÃÊý¾Ý¿â²Ù×÷

ÏÂһƪ£ºfastadmin CMSµÈϵÁвå¼þ°²×°²»³É¹¦µÄÎÊÌâ