What is the difference between function and array assignment?

Asked

Viewed 2,102 times

14

I have recently been coding in my projects where I need to add items in an array, so I don’t know if I should use native language functions for this, for example:

array_push($meu_array, 50);

or if I simply assign the value, for example:

$meu_array[] = 50;

I wonder what the difference is between them? if there is one that is more performative or more advisable to do, it would be interesting also to know if there is any parameter that I should take into account when a similar situation happens

  • 1

    Since you marked the javascript tag it is worth remembering that for such a language things are a little different, array_push is necessary to create the automatic indexes and the direct assignment under empty array will generate error.

3 answers

18


Both are not equivalent, array_push adds an item, and the $meu_array[0] = 50; would edit an existing item or create an index at zero.

The equivalent of array_push is the [] without the index, so:

$meu_array[] = 50;

However array_push has a difference

What differentiates array_push of [] is that with it you can add multiple items, so:

array_push($meu_array, 50, 60, 100, 9, 'a');

Whether it would be equivalent to:

$meu_array[] = 50;
$meu_array[] = 60;
$meu_array[] = 100;
$meu_array[] = 9;
$meu_array[] = 'a';

So in this case it might be more interesting to use array_push

Performance between [] and array_push

Supposedly the use of [] is a little better (is something relative), but is likely to be micro-optimization, ie the difference is insignificant, still yes to write I find it simpler to use the [] than array_push and beyond simple you write less:

$meu_array[] = 50;
array_push($meu_array, 50);

To compare the performance you can use the library phplegends/tests, requires composer, the ideal test is to do it in a large array, so only for testing $minha_array = range(0, 10000); that should be enough.

would look like this:

<?php
use PHPLegends\Tests\Bench;
use PHPLegends\Tests\BenchObject;

require 'vendor/autoload.php'; //Composer autoload

$minha_array1 = range(0, 10000);
$minha_array2 = range(0, 10000);

$bench = new Bench;
$bench->defaultCicles(9999); //9999 execuções

$test1 = $bench->addTest(function () use (&$minha_array1) {
    $minha_array1[] = 50;
});

$test2 = $bench->addTest(function () use (&$minha_array2) {
    array_push($minha_array2, 50);
});

$bench->run();

echo 'Test #1 (time): ', $test1->time(), PHP_EOL;
echo 'Test #2 (time): ', $test2->time(), PHP_EOL;

Result in PHP5.4:

Test #1 (time): 0.041198015213013
Test #2 (time): 0.051141023635864

Result in PHP7.2:

Test #1 (time): 0.0042929649353027
Test #2 (time): 0.0053369998931885

The difference in PHP7.2 seems insignificant, in other versions of PHP the result may be quite different, as in PHP5.4, or even vary between operating systems such as Windows and Linux

Anyway this is micro-optimization and usually we do not need to worry, except if you are really going to do thousands of executions, but for all other cases use whatever pleases you more.

Now multiple additions:

...
$test1 = $bench->addTest(function () use (&$minha_array1) {
    $minha_array1[] = 50;
    $minha_array1[] = 60;
    $minha_array1[] = 100;
    $minha_array1[] = 9;
    $minha_array1[] = 'a';
});

$test2 = $bench->addTest(function () use (&$minha_array2) {
    array_push($minha_array2, 50, 60, 100, 9, 'a');
});
...

Result in PHP5.4:

Test #1 (time): 0.053055047988892
Test #2 (time): 0.056273937225342

Result in PHP7.2:

Test #1 (time): 0.0079491138458252
Test #2 (time): 0.0065710544586182

Note that multiple inserts was a little better to use array_push, however the difference is minimal, in all tests, for 9999 repetitions it seems to be better, but probably the result varies a lot, ie there is no reason to worry between one and the other.

6

In these examples you cannot compare well. One adds an element to the array, or another assigns value to element 0 of array. They only do the same thing if the array is empty, so the semantics is different and you only get the same result by coincidence.

Obviously the first will always create a new element in the array increasing its size. The second will only create the element if index 0 does not exist.

The first will always add an element with a numerical index incremented with the largest number found. The second can use any key you want and doesn’t even need to be numerical.

According to a response in the OS the second is faster. I don’t know if it still holds for the current versions. Anyway it seems cleaner to do this way. And what says to documentation makes perfect sense.

There is a test in the documentation that shows a brutal difference. Remembering that this may not be true for the current versions. But there is another post showing a much smaller difference. And there’s another one showing that if you’re going to make several insertions, array_push() may be faster.

To get the same semantics the assignment would have to be like this:

$meu_array[] = 50;

I put in the Github for future reference.

4

The elements added with array_push() have only their numeric indices only. In the form of assignment it can be both numeric and associative.

$arr = array('e1' => 1, 'e2' => 2);
array_push($arr, 3);

Exit:

Array
(
    [e1] => 1
    [e2] => 2
    [0] => 3
)

Attributable:

$arr = array('e1' => 1, 'e2' => 2);
$arr['e3'] = 3;

Exit:

Array
(
    [e1] => 1
    [e2] => 2
    [e3] => 3
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.