In a for loop, I want to compute the maximum of values returned by a simple formula. I can write it either this way:
for( ... ){
val = ...; // a simple expression;
if(val > max_val)
max_val = val;
}
or this way:
for( ... ){
val = ...; // a simple expression;
max_val = max_val > val? max_val: val;
}
The only difference is that when max_val > val, no operation is done in the first code, but the second code ASSIGNS max_val to itself. Is this actually a real operation in CPU? In other words, are the two codes equal in performance?
I wonder how the compiler treats this code in both C/C++ and Java.
Aucun commentaire:
Enregistrer un commentaire