数组访问优化 [英] Array access optimization

查看:111
本文介绍了数组访问优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题真的。说我有在Java中的10×10阵列,一些地方在不使用,而我需要通过的所有元素去作为方法的一部分。它会更好为:

Just a quick question really. Say I have a 10x10 array in Java, some of the places in which are not used, and I'm needing to go through all elements as part of a method. Would it be better to:


  1. 通过所有的元素去与2循环和检查nulltype引起以避免错误,例如

  1. Go through all elements with 2 for loops and check for the nulltype to avoid errors, e.g.

for(int y=0;y<10;y++){
    for(int x=0;x<10;x++){
       if(array[x][y]!=null)
            //perform task here
    }
}


  • 或者,它会更好地让所有的使用的地址的列表...喂点的ArrayList

  • Or would it be better to keep a list of all the used addresses... Say an arraylist of points?

    不同的东西我没有提到的。

    Something different I haven't mentioned.

    我期待着任何答案:)

    I look forward to any answers :)

    推荐答案

    您尝试需求的任何解决方案必须在受控条件下类似尽可能的生产条件进行测试。由于Java的性质,你需要锻炼你的code位来获得可靠的性能统计数据,但我相信你已经知道了。

    Any solution you try needs to be tested in controlled conditions resembling as much as possible the production conditions. Because of the nature of Java, you need to exercise your code a bit to get reliable performance stats, but I'm sure you know that already.

    这表示,有几件事情,你可以尝试,我已经用于优化我的Java code成功(但不是在Android JVM

    This said, there are several things you may try, which I've used to optimize my Java code with success (but not on Android JVM)

    for(int y=0;y<10;y++){
        for(int x=0;x<10;x++){
           if(array[x][y]!=null)
                //perform task here
        }
    }
    

    应在任何情况下被修改成

    should in any case be reworked into

    for(int x=0;x<10;x++){
        for(int y=0;y<10;y++){
           if(array[x][y]!=null)
                //perform task here
        }
    }
    

    通常你会从缓存行引用获得的性能提升。让作为假设数组类型的富[] []

    for(int x=0;x<10;x++){
        final Foo[] row = array[x];
        for(int y=0;y<10;y++){
           if(row[y]!=null)
                //perform task here
        }
    }
    

    使用最后与变量应该帮助JVM优化code,但我认为,现代的JIT的Java编译器可以在许多情况下找出自己在code或不变量是否被改变。在另一方面,有时这可能更有效,但需要我们肯定进microoptimizations的境界:

    Using final with variables was supposed to help the JVM optimize the code, but I think that modern JIT Java compilers can in many cases figure out on their own whether the variable is changed in the code or not. On the other hand, sometimes this may be more efficient, although takes us definitely into the realm of microoptimizations:

    Foo[] row;
    for(int x=0;x<10;x++){
        row = array[x];
        for(int y=0;y<10;y++){
           if(row[y]!=null)
                //perform task here
        }
    }
    

    如果你不需要知道元素的索引,以在其上执行任务,你可以这样写的

    If you don't need to know the element's indices in order to perform the task on it, you can write this as

    for(final Foo[] row: array){
        for(final Foo elem: row
           if(elem!=null)
                //perform task here
        }
    }
    

    您可以尝试的另一件事就是击败阵列和存储在富[] 数组中的元素,确保参考最大局部性。你没有内环担心,但你需要做一些指数的算术特别引用数组元素时(而不是循环整个数组)。根据你做多久,它可能是或不是有利的。

    Another thing you may try is to flatten the array and store the elements in Foo[] array, ensuring maximum locality of reference. You have no inner loop to worry about, but you need to do some index arithmetic when referencing particular array elements (as opposed to looping over the whole array). Depending on how often you do it, it may or not be beneficial.

    由于大多数元素将不能为空的,保持它们作为一个稀疏数组是不适合你有利,因为你失去的局部性。

    Since most of the elements will be not-null, keeping them as a sparse array is not beneficial for you, as you lose locality of reference.

    的另一个问题是空测试。空测试本身成本并不高,但条件语句下面是这样,当你在code分支机构,失去时间对错误分支predictions。你可以做的就是用空对象,对其中的任务将有可能进行,但将达到非运算或者同样良性的。根据您要执行的任务时,它可能会或可能不会为你工作。

    Another problem is the null test. The null test itself doesn't cost much, but the conditional statement following it does, as you get a branch in the code and lose time on wrong branch predictions. What you can do is to use a "null object", on which the task will be possible to perform but will amount to a non-op or something equally benign. Depending on the task you want to perform, it may or may not work for you.

    希望这有助于。

    这篇关于数组访问优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆