What's the difference between using ArrayIterator vs Simple Array?

4

Some examples on the net with ArrayInterator are 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>";
}

I usually use this way:

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

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

What is the real performance difference when using ArrayIterator, would not it be a class to be instantiated?

Most of the examples just say that it is a modern way of programming in OOP, but it does not explain if it will impact memory usage.

    
asked by anonymous 24.09.2016 / 07:08

1 answer

1

In the test of exhaustion to verify the creation of instances and positions of memory with the code:

Note: Test taken of this php.net "http://php.net/manual/en_US/class.arrayiterator.php#class.arrayiterator"> ArrayIterator class .

<?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 noticed that results are minimalist in a general way, this test being done with% version > PHP and code taken from the site itself php.net , gave a certain victory to PHP primitive a>, array () , but with a difference very small:

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

In particular, I did not see any differences, of course an instance is more costly than a primitive type . If you need a more elaborate code and know how to use the ArrayIterator class (knowing how to work with Orientation Object mainly), you can use without fear that this will not be the problem of your code in relation to performance, but rather a set of factors:

  • bottleneck, missing keys, indexes, etc.
  • Emailed bad code, repetition, etc.
  • Class misuse and lack of knowledge in OOP.
  • Unnecessary and irrelevant codes to solve a particular problem.

There is a class that can be used in PHP's 5.6.22 version >=5.3.0 e 7 ", because it limits the number of items of this classe SplFixedArray , having a disadvantage that its indexes only accept integers and its advantage is about running faster ( performance ).

So, if you need to create a array with indexes of integers , this array would be the most ideal, for having a better performance.

    
25.09.2016 / 17:08