array.push和arr[]添加元素的快慢

I've done a small comparison between array_push() and the $array[] method and the $array[] seems to be a lot faster. 

<?php
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
    $array[] = $x;
}
?>

takes 0.0622200965881 seconds

and

<?php
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
    array_push($array, $x);
}
?>

takes 1.63195490837 seconds

so if your not making use of the return value of array_push() its better to use the $array[] way.

猜你喜欢

转载自blog.csdn.net/wangyingjie290107/article/details/88892813