问题
In PHP there are two ways to use an array as a stack (LIFO)
and two ways to use them as a queue (FIFO).
One could implement a stack with push & pop,
but the same can be done with unshift & shift.
Similarly one could implement a queue with push & shift,
but the same can be done with unshift & pop.
To demonstrate:
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";
Which outputs:
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
So which sets of functions to use?!
回答1:
Short answer
For stacks use push & pop (add to end, take from end).
For queues use push & shift (add to end, take from beginning).
Considerations
Documentation
When considering the PHP documentation as of today 2016-12-29 for these functions we find this:
For array_push():
the description mentions "array_push() treats array as a stack"
and the example uses "$stack".
For array_pop():
the description mentions nothing on stacks or queues
and the example uses "$stack".
For array_shift():
the description mentions nothing on stacks or queues (but mentions required reindexing)
and the example uses "$stack".
For array_unshift():
the description mentions nothing on stacks or queues (but mentions required reindexing)
and the example uses "$queue".
So this suggests using push & pop for stacks (based on mention in description)
and suggests using unshift & pop for queues (based on only mention of queue in examples).
This seems a bit thin... A suggestion to improve the documentation has been submitted here: https://bugs.php.net/bug.php?id=73839
Performance
Running this:
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);
Returns this:
stack push & pop approach : 0.011210918426514
stack unshift & shift approach: 0.015399217605591
queue push & shift approach : 0.011627912521362
queue unshift & pop approach : 0.015273094177246
For stacks this suggests to use push & pop: natural terminology, matching the mention in the documentation and also performing better (which makes sense considering the reindexing with both unshift & shift).
For queues this suggests to use push & shift, despite the mention in the documentation.
来源:https://stackoverflow.com/questions/41387092/php-pop-push-shift-unshift-which-to-use-for-queues-and-which-for-stacks