System.arraycopy 数组copy要比 for循环快

2018-03-09  本文已影响9人  ElonYanJ

'''
char[] rest = new char[1000000];
for (int a = 0; a < 1000000; a++) {
rest[a] = 'a';
}

    long start1 = System.currentTimeMillis();
    int lengthSubstract = rest.length;
    char[] nextRest = new char[lengthSubstract - 1];
    System.arraycopy(rest, 0, nextRest, 0, lengthSubstract - 1);
    long end1 = System.currentTimeMillis() - start1;
    Log.d("ddddssss", end1 + "");

    long start2 = System.currentTimeMillis();
    int lengthSubstract2 = rest.length - 1;
    char[] nextRest2 = new char[lengthSubstract2];
    for (int a = 0; a < lengthSubstract2; a++) {
        nextRest2[a] = rest[a];
    }
    long end2 = System.currentTimeMillis() - start2;
    Log.d("ddddssss", end2 + "");

'''

上一篇 下一篇

猜你喜欢

热点阅读