Home > Back-end >  Double for loop to run a matter of time
Double for loop to run a matter of time

Time:10-07

Public class ArryDemo {
Public int [] [] a;
Public int [] [] b;

Public static void main (String [] args) {
//TODO automatically generated method stub
ArryDemo a1=new ArryDemo ();
A1. The news ();
Long startTime1=System. CurrentTimeMillis ();
//initialize
A1. News2 ();
Long endTime1=System. CurrentTimeMillis ();
System. Out. Println (" initialization running time: "+ (endTime1 - startTime1)," ms ");
Long startTime2=System. CurrentTimeMillis ();
//column initializes the
A1. News3 ();
Long endTime2=System. CurrentTimeMillis ();
System. Out. Println (" column initializes the runtime: "+ (endTime2 - startTime2)," ms ");
}

/* *
* constructor will a, b array is set to 128 line 40960 columns
*/
Public ArryDemo () {
A=new int [128] [40960].
B=new int [128] [40960].
}

/* *
* initialize the array b
*/
Public void news () {
For (int I=0; I & lt; 128; I++) {
For (int j=0; J & lt; 40960; J++) {
B [I] [j]=100;
}
}
}

/* *
* will b in the way an array of values is assigned to a array
*/
Public void news2 () {
For (int I=0; I & lt; 128; I++) {
For (int j=0; J & lt; 40960; J++) {
[I] [j]=b [I] [j];
}
}
}

/* *
* will be in the form of column of the array b value is assigned to a array
*/
Public void news3 () {
For (int j=0; J & lt; 40960; J++) {
For (int I=0; I & lt; 128; I++) {
[I] [j]=b [I] [j];
}
}
}
}
Code as above, the outer for loop to run, the more the more time-consuming, write the same on the matlab program, however, is the opposite results: the outer for loop to run, the more the more save time
Why is that? (this is a multi-core programming class homework, I didn't find the reason, please help)
  • Related