java - Performance of 2D array allocation -
java - Performance of 2D array allocation -
i wondering why allocation of 2d int array @ 1 time (new int[50][2]
) performs poorer allocating separately, is, execute new int[50][]
first, new int[2]
one-by-one. here non-professional benchmark code:
public class allocationspeed { private static final int iteration_count = 1000000; public static void main(string[] args) { new allocationspeed().run(); } private void run() { measureseparateallocation(); measureallocationatonce(); } private void measureallocationatonce() { stopwatch stopwatch = stopwatch.createstarted(); (int = 0; < iteration_count; i++) { allocateatonce(); } stopwatch.stop(); system.out.println("allocate @ once: " + stopwatch); } private int allocateatonce() { int[][] array = new int[50][2]; homecoming array[10][1]; } private void measureseparateallocation() { stopwatch stopwatch = stopwatch.createstarted(); (int = 0; < iteration_count; i++) { allocateseparately(); } stopwatch.stop(); system.out.println("separate allocation: " + stopwatch); } private int allocateseparately() { int[][] array = new int[50][]; (int = 0; < array.length; i++) { array[i] = new int[2]; } homecoming array[10][1]; } }
i tested on 64 bit linux, these results different 64 bit oracle java versions:
1.6.0_45-b06:
separate allocation: 401.0 ms allocate @ once: 1.673 s
1.7.0_45-b18
separate allocation: 408.7 ms allocate @ once: 1.448 s
1.8.0-ea-b115
separate allocation: 380.0 ms allocate @ once: 1.251 s
just curiosity, tried openjdk 7 (where difference smaller):
separate allocation: 424.3 ms allocate @ once: 1.072 s
for me it's quite counter-intuitive, expect allocating @ 1 time faster.
absolute unbelievable. benchmark source might suffer optimizations, gc , jit, this?
looking @ java byte code instruction set:
anewarray (+ 2 bytes indirect class index) arrays of object classes (a = address) newarray (+ 1 byte prinitive class) arrays of primitive types multianewarray (+ 2 bytes indirect class index) multidimensional arraysthis leads 1 suspect multianewarray suboptimal primitive types.
before looking further, hope knows misled.
java arrays performance
Comments
Post a Comment