简体   繁体   English

多维数组中元素访问的效率

[英]Efficiency of accessing element in multi-dimensional array

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? 在php中使用多维数组(或与此相关的任何语言)时,将多维数组的数组另存为单独的数组,或者直接从整个数组访问该“数组”元素,您是否会获得任何效率和生产力?

For example, 例如,
print_r($myArray); 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? 如果我需要重复访问[16.0]数组,那么最好将该条目另存为自己的数组,直到不再需要它为止,还是直接访问它会更好?

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? 当然,这只是一个小例子,但如果多次访问(任意)$ array [。] [。] [ n ] [...]中的值,并且n的值取决于循环,直接访问元素或将数组(深入到数组的各个层)保存为自己的数组与以这种方式访问​​其值之间有什么区别吗?

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). (我已经重新格式化并删除了EXT_STMT标记)。 You can see that the only difference is the actual assignment to $tempArray. 您可以看到唯一的区别是对$ 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. 但是,PHP使用引用模型并执行了写入时的懒惰复制,因此它不是这样做的,并且无论数组大小如何,其成本都几乎相同。

(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.) (当然,除非您在assignemt之后更改元素,否则$ tempArray不再与它的原始切片相同,这时内存使用量会随着分配触发切片的克隆而跳转,因为需要拆分引用。 )

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). 好的,如果您要对切片进行大量的本地化只读访问以保存重复的FETCH_DIM_R查找(PHP编译绝对不会对索引的重复使用进行本地优化),则此方法可能值得。 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() ? 为什么不使用循环以及microtime()memory_get_usage()对自己进行基准测试?

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. 您可以通过使用for循环创建一个巨大的多维数组来轻松地对此进行测试,但是在索引大型数组时直接访问它通常会更快。

With smaller arrays (~500 items or less) it isn't going to make a noticeable difference. 对于较小的数组(约500个项目或更少),这不会产生明显的变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM