Is Java's System.arraycopy() efficient for small arrays?

前端 未结 7 2012
独厮守ぢ
独厮守ぢ 2020-12-02 13:01

Is Java\'s System.arraycopy() efficient for small arrays, or does the fact that it\'s a native method make it likely to be substantially less efficient than a s

7条回答
  •  感情败类
    2020-12-02 13:30

    Here is my benchmark code:

    public void test(int copySize, int copyCount, int testRep) {
        System.out.println("Copy size = " + copySize);
        System.out.println("Copy count = " + copyCount);
        System.out.println();
        for (int i = testRep; i > 0; --i) {
            copy(copySize, copyCount);
            loop(copySize, copyCount);
        }
        System.out.println();
    }
    
    public void copy(int copySize, int copyCount) {
        int[] src = newSrc(copySize + 1);
        int[] dst = new int[copySize + 1];
        long begin = System.nanoTime();
        for (int count = copyCount; count > 0; --count) {
            System.arraycopy(src, 1, dst, 0, copySize);
            dst[copySize] = src[copySize] + 1;
            System.arraycopy(dst, 0, src, 0, copySize);
            src[copySize] = dst[copySize];
        }
        long end = System.nanoTime();
        System.out.println("Arraycopy: " + (end - begin) / 1e9 + " s");
    }
    
    public void loop(int copySize, int copyCount) {
        int[] src = newSrc(copySize + 1);
        int[] dst = new int[copySize + 1];
        long begin = System.nanoTime();
        for (int count = copyCount; count > 0; --count) {
            for (int i = copySize - 1; i >= 0; --i) {
                dst[i] = src[i + 1];
            }
            dst[copySize] = src[copySize] + 1;
            for (int i = copySize - 1; i >= 0; --i) {
                src[i] = dst[i];
            }
            src[copySize] = dst[copySize];
        }
        long end = System.nanoTime();
        System.out.println("Man. loop: " + (end - begin) / 1e9 + " s");
    }
    
    public int[] newSrc(int arraySize) {
        int[] src = new int[arraySize];
        for (int i = arraySize - 1; i >= 0; --i) {
            src[i] = i;
        }
        return src;
    }
    

    From my tests, calling test() with copyCount = 10000000 (1e7) or greater allows the warm-up to be achieved during the first copy/loop call, so using testRep = 5 is enough; With copyCount = 1000000 (1e6) the warm-up need at least 2 or 3 iterations so testRep shall be increased in order to obtain usable results.

    With my configuration (CPU Intel Core 2 Duo E8500 @ 3.16GHz, Java SE 1.6.0_35-b10 and Eclipse 3.7.2) it appears from the benchmark that:

    • When copySize = 24, System.arraycopy() and the manual loop take almost the same time (sometimes one is very slightly faster than the other, other times it’s the contrary),
    • When copySize < 24, the manual loop is faster than System.arraycopy() (slightly faster with copySize = 23, really faster with copySize < 5),
    • When copySize > 24, System.arraycopy() is faster than the manual loop (slightly faster with copySize = 25, the ratio loop-time/arraycopy-time increasing as copySize increases).

    Note: I’m not English native speaker, please excuse all my grammar/vocabulary errors.

提交回复
热议问题