why is java taking long time initializing two dimensional arrays starting with the first dimension having a big size number?

▼魔方 西西 提交于 2020-01-24 04:01:21

问题


I have noticed that initializing 2D array like this

case 1 :-

int ar [] [] = new int [10000001][10] ;

taking more time than initializing it like this

case 2 :-

int ar[] [] = new int [10] [10000001] ;

in case 1 it toke time around 4000ms but in case 2 it does not exceed 100ms why there is this big gap ?


回答1:


Strictly speaking, Java does not have 2D arrays: instead, it uses 1D arrays arranged into 1D arrays of arrays.

In your first case, in addition to the single array of arrays, Java makes 10000001 arrays of 10 elements, while in the second case it makes 10 arrays of 10000001 elements.

Since the number of objects differs by a factor of million, the first case is significantly slower.



来源:https://stackoverflow.com/questions/43471708/why-is-java-taking-long-time-initializing-two-dimensional-arrays-starting-with-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!