What is the difference between using Arrayiterator vs Simple Array?

Asked

Viewed 192 times

4

Some examples on the net with ArrayInterator is used in the following ways::

$arr = array("Banana", "Abacaxi", "Abacate", "Morango");    

// loop through the object
foreach (new ArrayIterator($arr) as $key => $value) {
    echo $key . ": " . $value . "<br>";
}

or

$arr = array("Banana", "Abacaxi", "Abacate", "Morango");

$iter = new ArrayIterator($arr);

// loop through the object
foreach ($iter as $key => $value) {
    echo $key . ": " . $value . "<br>";
}

Usually use it like this:

$arr = array("Banana", "Abacaxi", "Abacate", "Morango");

// loop through the object
foreach ($arr as $key => $value) {
    echo $key . ": " . $value . "<br>";
}

What is the real difference in performance when using Arrayiterator, would no longer be a class to be instantiated?

Most examples just say it’s a modern way of programming in OOP, but it doesn’t explain, whether it will impact memory usage.

  • 1

    This question about memory/performance consumption depends on the purpose of the use and how the resources are used. In this other question you can see how misuse influences performance. And as the correct use, within an appropriate context, can result in a better performance when working with OOP http://answall.com/questions/149808/comparando-se-com-classe-datetime-a-fun%C3%A7%C3%A3o-date-%C3%A9-more-perform%C3%A1tica/149869#149869

  • @Danielomine Blz, I’ll take a look, but I would like to better understand the use of this, I’ll give you another search! Thanks.

1 answer

1


In exhaust test to check the creation of instances and memory positions with the code:

Observing: Test taken from this link of the site php.net with modification to create class Arrayiterator.

<?php
echo "<p>".phpversion()."</p>";

for($size = 1000; $size < 50000000; $size *= 2)
{        

    echo "<p>" . "Testing size: $size" . "</p>";

    for($s = microtime(true), $container = Array(), $i = 0; $i < $size; $i++)
        $container[$i] = NULL;
    echo "<p>Array(): " . (microtime(true) - $s) . "</p>";        

    for($s = microtime(true), $container = new ArrayIterator(), $i = 0; $i < $size; $i++)
        $container[$i] = NULL;
    echo "<p>ArrayInterator(): " . (microtime(true) - $s) . "</p>";


    echo "==========================================<br />";
}

I checked that the results are minimalist in general, this test is carried out with PHP version 5.6.22 and code taken from the site itself php.net, gave a certain victory to the guy primitive PHP, the array(), but with a very small difference:

PHP version 5.6.22

Testing size: 1000

Array(): 0.00023007392883301

Arrayinterator(): 0.00017189979553223

========================================== Testing size: 2000

Array(): 0.00038886070251465

Arrayinterator(): 0.00034308433532715

========================================== Testing size: 4000

Array(): 0.00078988075256348

Arrayinterator(): 0.00084090232849121

========================================== Testing size: 8000

Array(): 0.0017249584197998

Arrayinterator(): 0.0018649101257324

========================================== Testing size: 16000

Array(): 0.0036230087280273

Arrayinterator(): 0.0040380954742432

========================================== Testing size: 32000

Array(): 0.0067539215087891

Arrayinterator(): 0.0051989555358887

========================================== Testing size: 64000

Array(): 0.0097739696502686

Arrayinterator(): 0.011684894561768

========================================== Testing size: 128000

Array(): 0.021622180938721

Arrayinterator(): 0.023998975753784

========================================== Testing size: 256000

Array(): 0.043958902359009

Arrayinterator(): 0.047860860824585

========================================== Testing size: 512000

Array(): 0.088205814361572

Arrayinterator(): 0.097460985183716

========================================== Testing size: 1024000

Array(): 0.1791729927063

Arrayinterator(): 0.20163583755493

========================================== Testing size: 2048000

Particularly, I saw no differences, of course a instance and more expensive than a primitive type. If you need a more elaborate code and know how to use the class Arrayiterator (know how to work with Object Orientation mainly), can use without fear that this will not be the problem of your code in relation to performance, but rather a set of factors:

  • bank neck, missing keys, indexes, etc.
  • elaborate bad code, repetition, etc.
  • misuse of classes and lack of knowledge in POO.
  • unnecessary and irrelevant codes to solve a particular problem.

There is, a class that can be used in the versions >=5.3.0 e 7 from PHP to classe SplFixedArray, because it limits the number of items in that array, having a downside that your indexes only accept whole numbers and your head start is about the execution being faster (performance).

So if you need to create a array with indexes of integer, that one classe SplFixedArray would be the most ideal, for having a better performance.

Browser other questions tagged

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