项目中发现
查看手册
运行代码 比较数组队列 和 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->enqueue($data);
if ($i % 100 == 99 and count($splq) > 100)
{
$popN = rand(10, 99);
for ($j = 0; $j < $popN; $j++)
{
$splq->dequeue();
}
}
}
list($t1, $t2) = explode(' ', microtime());
$et = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
echo $et - $st;
echo "\n";
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;
echo "\n";
果然差距还是相当大
参考文章:www.cnblogs.com/itsuibi/p/10870608...
数组每次出队入队内存地址都会移动会很慢,php就用Splqueue类来实现队列。
具体Splqueue怎么实现的,就下回再学习了,简单记录一下。