Compare two ways to set / return an array:
static public float[] test_arr_speeds_1( int a ) {
return new float[]{ a, a + 1, a + 2, a + 3, a + 4, a + 5,
a + 6, a + 7, a + 8, a + 9 };
}
static public float[] test_arr_speeds_2( int a ) {
float[] ret = new float[10];
ret[0] = a;
ret[1] = a + 1;
ret[2] = a + 2;
ret[3] = a + 3;
ret[4] = a + 4;
ret[5] = a + 5;
ret[6] = a + 6;
ret[7] = a + 7;
ret[8] = a + 8;
ret[9] = a + 9;
return ret;
}
Both generate different bytecodes, and both can be decompiled in their previous state. After checking the execution time through the profiler (iterations 100M, unbiased, different neighborhoods), the time of the _1 method is approx. 4/3 time _2, although both create a new array and both set each field to a given value. The time in most cases is negligible, but it still bothers me - why is _1 noticeably slower? Can someone check / confirm / explain this to me in a reasonable way supported by the JVM?
source
share