【问题标题】:php pop/push/shift/unshift, which to use for queues and which for stacksphp pop/push/shift/unshift,哪个用于队列,哪个用于堆栈
【发布时间】:2023-03-04 21:37:01
【问题描述】:

在 PHP 中有两种方法可以将数组用作 堆栈 (LIFO)
以及将它们用作队列 (FIFO) 的两种方式。

可以使用pushpop 实现堆栈
但是unshift & shift 也可以做到这一点。

同样可以使用pushshift 实现队列
但是unshift & pop 也可以做到这一点。

演示:

echo "stack push & pop approach:\n";
$s = []; array_push($s, 'first'); array_push($s, 'second'); array_push($s, 'third');
echo     array_pop($s) . '-' .    array_pop($s) . '-' .     array_pop($s) .  "\n";
echo "stack unshift & shift approach:\n";
$s = []; array_unshift($s, 'first'); array_unshift($s, 'second'); array_unshift($s, 'third');
echo     array_shift($s) . '-' .     array_shift($s) . '-' .      array_shift($s) .  "\n";
echo "queue push & shift approach:\n";
$q = []; array_push($q, 'first'); array_push($q, 'second'); array_push($q, 'third');
echo     array_shift($q) . '-' .  array_shift($q) . '-' .   array_shift($q) .  "\n";
echo "queue unshift & pop approach:\n";
$q = []; array_unshift($q, 'first'); array_unshift($q, 'second'); array_unshift($q, 'third');
echo     array_pop($q) . '-' .       array_pop($q) . '-' .        array_pop($q) .       "\n";

哪些输出:

stack push & pop approach:
third-second-first
stack unshift & shift approach:
third-second-first
queue push & shift approach:
first-second-third
queue unshift & pop approach:
first-second-third

那么使用哪组函数呢?!

【问题讨论】:

    标签: php stack queue


    【解决方案1】:

    简答

    堆栈使用push & pop(添加到末尾,从末尾获取)。

    队列使用push & shift(添加到末尾,从头开始)。

    注意事项

    文档

    在考虑截至 2016 年 12 月 29 日的 PHP 文档时,我们发现这些函数:

    对于array_push():
    描述提到“array_push() 将数组视为堆栈
    示例使用“$stack”。

    对于array_pop():
    描述没有提到堆栈或队列
    示例使用“$stack”。

    对于array_shift():
    描述没有提到堆栈或队列(但提到需要重新索引)
    示例使用“$stack”。

    对于array_unshift():
    描述没有提到堆栈或队列(但提到需要重新索引)
    示例使用“$queue”。

    所以这建议使用 pushpop 用于堆栈(基于描述中的提及)
    并建议对队列使用unshiftpop(仅基于示例中提到的队列)。

    这似乎有点薄......改进文档的建议已在此处提交:https://bugs.php.net/bug.php?id=73839

    性能

    运行这个:

    echo "\nstack push & pop approach     :  ";
    $a=[];$i=0;$t=microtime(true); while($i<100000) { array_push($a, 'dummy'); array_pop($a); ++$i; }; echo (microtime(true) - $t);
    echo "\nstack unshift & shift approach:  ";
    $a=[];$i=0;$t=microtime(true); while($i<100000) { array_unshift($a, 'dummy'); array_shift($a); ++$i; }; echo (microtime(true) - $t);
    echo "\nqueue push & shift approach   :  ";
    $a=[];$i=0;$t=microtime(true); while($i<100000) { array_push($a, 'dummy'); array_shift($a); ++$i; }; echo (microtime(true) - $t);
    echo "\nqueue unshift & pop approach  :  ";
    $a=[];$i=0;$t=microtime(true); while($i<100000) { array_unshift($a, 'dummy'); array_pop($a); ++$i; }; echo (microtime(true) - $t);
    

    返回这个:

    stack push & pop approach     :  0.011210918426514
    stack unshift & shift approach:  0.015399217605591
    queue push & shift approach   :  0.011627912521362
    queue unshift & pop approach  :  0.015273094177246
    

    对于 stacks,这建议使用 pushpop:自然术语,与文档中的提及相匹配并且性能也更好(考虑到使用 unshift 重新索引是有道理的&shift)。

    对于queue,尽管文档中有提及,但建议使用pushshift

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-26
      • 2013-03-21
      • 2021-09-06
      • 2016-08-16
      • 2013-04-20
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多