Efficient System.arraycopy on multidimensional arrays

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

    In my use case this

    public  T[][] arrayCopy(T[][] array) {
    
        return Arrays.stream(array).
                  map(el -> el.clone()).toArray(a -> array.clone());
    }
    

    was way faster than using System.arraycopy or the straight forward two for loops solution.
    Also tested with primitives ( arrayCopy(int[][] array) ) and got the same result.

提交回复
热议问题