Efficient System.arraycopy on multidimensional arrays

后端 未结 5 421
花落未央
花落未央 2020-12-16 02:34

I\'m aware that a common performance refactoring is to replace simple for\'s by System.arraycopy.

I want to ask about:

  1. Whe

5条回答
  •  轮回少年
    2020-12-16 02:53

    Like all performance timing questions, you really need to benchmark in the environment where you are expecting to run your code. Different JVM versions and hardware (CPU, memory, etc) configurations may have different results. It really depends on your specific performance requirements.

    But then, before getting to this level of performance tuning, you should first write the code clearly and make it right first. The compiler and JIT will be able to to a lot of optimizations for you with normal algorithm expressions but sometimes hand optimizations can confuse these automated optimizations. After you have a working product and if performance is not then what you would like, then profile and work on the hot spots only. (Though sometimes for more complex code you may need to refactor and/or change your logic.)

    In this case, if you copying entire arrays then use System.arraycopy as this is the standard way to do that. The compiler may now or in the future provide additional optimizations for that since the core APIs rely on this heavily you can rest assured that this is something that the JVM developers always want to have running optimally.

    You will need to run through some loops though since System.arraycopy can only do a single array object and with Java multi-dimension arrays are really arrays of arrays. So...

    public int[][][] copyOf3Dim(int[][][] array) {
        int[][][] copy;
        copy = new int[array.length][][];
        for (int i = 0; i < array.length; i++) {
            copy[i] = new int[array[i].length][];
            for (int j = 0; j < array[i].length; j++) {
                copy[i][j] = new int[array[i][j].length];
                System.arraycopy(array[i][j], 0, copy[i][j], 0, 
                    array[i][j].length);
            }
        }
        return copy;
    }    
    

    Or you could use Arrays.copyOf which uses System.arraycopy and some reflection internally (so is not as fast as directly using System.arraycopy yourself), but does not do a deep copy.

提交回复
热议问题