0

I can't see to find an answer for this, but it's been bugging me for a while.

When using multidimensional arrays in php (or any language for that matter), do you get any efficiency and productivity out of saving an array of a multidimensional array as its separate array or accessing that "array" element directly from the whole array?

For example,
print_r($myArray);

Array
(
   [2] => Array
      (
         [7.0] => Array
            (
               [0] => 1
               [1] => 23
            )

         [16.0] => Array
            (
               [0] => 4
               [1] => 28
            )
      )
   [5] => Array
      (
         [17.0] => Array
            (
               [0] => 1
               [1] => 3
            )
      )
)   

If I needed to REPEATABLY access the array of [16.0], would it be better to just save that entry as its own array until I don't need it anymore, or is it better just to access it directly?

Option 1:
$tempArray=$myArray[2]["16.0"];
echo "Index=".$tempArray[0].";

or

Option 2:
echo "Index=".$myArray[2]["16.0"][0];


Of course this is just a tiny example, but if the values in (arbitrary) $array[.][.][n][...] are being accessed more than once, and the value of n depends on the index of a loop, is there any difference between accessing the element directly or saving that array (deep down on the layers of the array) as its own array and accessing its values that way?

2 Answers 2

2

If you look at the bytecode generated for these statements:

$tempArray=$myArray[2]["16.0"];        
echo $tempArray[0];        
    FETCH_DIM_R        $3      $myArray, 2        
    FETCH_DIM_R        $4      $3, '16.0'        
    ASSIGN                     $tempArray, $4        
    FETCH_DIM_R        $6      $tempArray, 0        
    ECHO               $6        

echo $myArray[2]["16.0"][0];        
    FETCH_DIM_R        $7      $myArray, 2        
    FETCH_DIM_R        $8      $7, '16.0'        
    FETCH_DIM_R        $9      $8, 0        
    ECHO               $9        

(I've reformatted and removed the EXT_STMT markers). You can see that the only difference is the actual assignment to $tempArray. You might think that an array assignment is expensive. However PHP uses a referencing model and does a lazy copy-on-write, so it is not, and the cost is the pretty much same whatever the array size.

(Except of course if you change elements after the assignemt, so $tempArray is no longer the same as its originating slice, and at that point the memory usage jumps as the assign triggers the clone of the slice as the references need to be split.)

OK, this approach might be worthwhile if you are doing a lot of localised readonly access to a slice to save the repeated FETCH_DIM_R lookups (the PHP compile does absolutely no local optimisation of repeated use of indexes). However, the opportunities to shoot yourself in the foot on update are significant.

Why not benchmark this yourself using a loop and microtime() and memory_get_usage()?

Sign up to request clarification or add additional context in comments.

Comments

1

You can easily test this yourself by creating an immense multidimensional array with a for loop, but accessing it directly will generally be faster when indexing large arrays.

With smaller arrays (~500 items or less) it isn't going to make a noticeable difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.