Time complexity of System.arraycopy(…)?

前端 未结 5 1605
借酒劲吻你
借酒劲吻你 2020-12-01 09:18

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) is a native method.

What is the time complexity for this method?

5条回答
  •  囚心锁ツ
    2020-12-01 09:54

    Just to sum up relevant comments on another question (flagged as a duplicate of this one).

    Surely, it's just adding to the new array with all the entries of the other? Which would be O(n) where n is the number of values to be added.

    bragboy's answer agrees of course, but then I had thought that the only way to get a sure answer was to find the source code to get a canonical answer, but that isn't possible. Here is the declaration for System.arraycopy();

    public static native void arraycopy(Object src, int src_position,  
                                        Object dst, int dst_position,  
                                        int length);
    

    It's native, written in the language of the operating system, which means that the implementation of arraycopy() is platform dependant.

    So, in conclusion it's likely O(n), but maybe not.

提交回复
热议问题