M
Martin
Hi,
Just a question of efficiency really. Here is a bit of a psudo noddy
example but I was wondering about how this might work in reality;
int[][] myArray = new int[1000][1000];
for(int i = 0; i < MaxValue(myArray); i++){
.....do something
}
private int MaxValue(int[][] anArray){
int max = 0;
for(int x = 0; x < anArray.length; x++){
for(int y = 0; y < anArray[0].length; y++){
if(anArray[x][y] > max){
max = anArray[x][y];
}
} // end y
} // end x
return max;
} // end MaxValue
Please excuse any typos, I only intend to illustrate what queries I
have.
I have the following questions about efficiency;
1) Every time the i loop is run, will it have to run the MaxValue()
method? I am expecting that it does but I am unsure.
2) I have read that comparing values from arrays are slower than
primitive type variables. Would something like this be more
appropriate for the MaxValue() method;
for(int x = 0; x < anArray.length; x++){
for(int y = 0; y < anArray[0].length; y++){
int tempValue = anArray[x][y];
if(tempValue > max){
max = tempValue;
}
} // end y
} // end x
3) Is there a performance difference between loops, e.g. for or while
loops
I know normally these efficiency considerations dont matter when
dealing with little amounts of data etc, however I have a very large
program I am developing to deal with Satellite Imagery, so the time
taken to do some of these things grows quite considerably.
Thank you,
Martin
Just a question of efficiency really. Here is a bit of a psudo noddy
example but I was wondering about how this might work in reality;
int[][] myArray = new int[1000][1000];
for(int i = 0; i < MaxValue(myArray); i++){
.....do something
}
private int MaxValue(int[][] anArray){
int max = 0;
for(int x = 0; x < anArray.length; x++){
for(int y = 0; y < anArray[0].length; y++){
if(anArray[x][y] > max){
max = anArray[x][y];
}
} // end y
} // end x
return max;
} // end MaxValue
Please excuse any typos, I only intend to illustrate what queries I
have.
I have the following questions about efficiency;
1) Every time the i loop is run, will it have to run the MaxValue()
method? I am expecting that it does but I am unsure.
2) I have read that comparing values from arrays are slower than
primitive type variables. Would something like this be more
appropriate for the MaxValue() method;
for(int x = 0; x < anArray.length; x++){
for(int y = 0; y < anArray[0].length; y++){
int tempValue = anArray[x][y];
if(tempValue > max){
max = tempValue;
}
} // end y
} // end x
3) Is there a performance difference between loops, e.g. for or while
loops
I know normally these efficiency considerations dont matter when
dealing with little amounts of data etc, however I have a very large
program I am developing to deal with Satellite Imagery, so the time
taken to do some of these things grows quite considerably.
Thank you,
Martin