Traversal performance of multidimensional arrays

Source: Internet
Author: User

An array is a container object in Java that has multiple values of a single type. The array length is determined when the array is created. After it has been created, its length is fixed. The following is an array of length 10:

 Public class Arraydemo {    privateint arraysize=10;      Public int New int [arraySize];}

The above code is an example of a one-dimensional array. In other words, the array length can only grow in one direction. Many times we need arrays to grow on multiple dimensions. This array we call multidimensional arrays. For simplicity, we call it a 2-dimensional array. A two-dimensional array is useful when we need a matrix or x-y coordinate system. Here is an example of a two-dimensional array:

 Public class Theproblemof2darray {    privatestaticfinalint arr_size=10;      Public Static void Main (string[] args) {        int arr[][]=newint[arr_size][arr_size ];    }}

Imagine a two-dimensional array that looks like a matrix of an X-y coordinate system.

However, it may be surprising to Java developers that Java does not actually have a two-dimensional array.

In a real array, all the elements are stored in memory in contiguous chunks of memory, but not in a two-dimensional array in Java. The elements in all one-dimensional arrays in Java occupy adjacent memory locations and are therefore a true array.

In Java, when we define:

int singleelement//denotes an int variable
Int[] Singledarray//denotes an array of int variables (one-dimensional)
Int[][] Twodarray//Represents an array of array of int variables (two-dimensional)

This means that, in the example above, a two-dimensional array is a reference to an array, and each of its elements is a reference to another int array.

This picture clearly explains the concept.

Because the two-dimensional arrays are scattered in memory, there are some effects on performance. To analyze this discrepancy, I wrote a simple Java program that shows the importance of the traversal order.


/*** Problems with two-dimensional arrays * * We are initializing a 2-dimensional array of any size. (To simplify the analysis we use two-dimensional square matrices) we will iterate over the same array in a couple of different ways, and the performance gap between the two iterations of the analysis is large * *@authorMohit **/ Public classTheproblemof2darray {//Array Size: The larger the array, the more noticeable the performance gap Private Static Final intArr_size = 9999; Public Static voidMain (string[] args) {//new Array intArr[][] =New int[Arr_size][arr_size]; LongCurrtime =System.currenttimemillis (); Colmajor (arr); System.out.println ("Total time in Colmajor:" + (System.currenttimemillis ()-Currtime) + "MS"); //new array, exactly the same as Arr intArr1[][] =New int[Arr_size][arr_size]; Currtime=System.currenttimemillis (); Rowmajor (ARR1); //The only difference in aboveSYSTEM.OUT.PRINTLN ("Total time in col:" + (System.currenttimemillis ()-Currtime) + "MS"); } /*** The following code iterates through the array as a column first scans the column before the next column is scanned **/ Private Static voidColmajor (intarr[][]) { for(inti = 0; i < arr_size; i++) { for(intj = 0; J < Arr_size; J + +) { //See this , we is traversing J first and then IARR[I][J] = i +J; } } } /*** If we switch the internal and external loop program to traverse the array in line precedence, scan the bank before scanning the next line * This means that when we access the array each time we access different columns (and therefore also access different pages) The small changes in code will cause the program to take more time to complete the traversal */ Private Static voidRowmajor (intarr[][]) { for(inti = 0; i < arr_size; i++) { for(intj = 0; J < Arr_size; J + +) { /** Look at this, we'll go through J, then walk through I, but they're farther away from the element, so it takes more*/Arr[j][i]= i +J; } } }}

Here is the result of the operation:

Total time in colmajor:1332036 ms

You can see the performance gap is still very large, so in the operation of the two-dimensional array in order to operate.

Traversal performance of multidimensional arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.