Efficient System.arraycopy on multidimensional arrays

后端 未结 5 422
花落未央
花落未央 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:56

    It's not difficult to use System.arraycopy to do a quick deep copy. Here's an example for a 2D array:

    for (int i = 0; i < src.length; i++) {
        System.arraycopy(src[i], 0, dest[i], 0, src[0].length);
    }
    

    From a quick timing test, using this to copy a 1000x1000 2D array 100 times takes 40 milliseconds, versus 1740 milliseconds using the more obvious two for loops and assignment.

提交回复
热议问题