Is array_push() as efficient as creating an array with elements inside it

时间:2016-04-07 10:41:43

标签: php performance

I found some old code that i created a while back when I first started programming, I was pushing 50+ elements into an array rather then creating them inside the array at the start.

Am i effecting performance when I push multiple elements into my array rather then just create the array containing the elements at the start.

2 个答案:

答案 0 :(得分:0)

  1. If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
    1. array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.

plz check link

http://php.net/manual/en/function.array-push.php

答案 1 :(得分:0)

array_push()$array[]慢。在PHP docs,有人做了一个很好的性能测试。

我猜是因为内部实现。可能每次都会创建一个新数组和/或分配新内存,这会使它更慢。但我们谈论的是数百或数千条记录。任何不足都不会对绩效产生明显影响。