I'd like to know if it costs more to access several times a value at array[i] or to stock the value as a new variable (v=array[i]) and access the variable v several times instead ?
for example (in java), would it be better to write :
int [] array = {1,2,3,4,5};
for(int i=0;i<5;i++){
if (array[i]<0){
System.out.println("negativ");
}else if(array[i]>0){
System.out.println("positiv");
if (array[i]==42){
System.out.println("great answer");
}
}else{
System.out.println("zero");
}
}
or
int [] array = {1,2,3,4,5};
int v;
for(int i=0;i<5;i++){
v = array[i];
if (v<0){
System.out.println("negativ");
}else if(v>0){
System.out.println("positiv");
if (v==42){
System.out.println("great answer");
}
}else{
System.out.println("zero");
}
}
Thank you for your help.