SunのJDKで提供されているJavaコンパイラが「スマート」であることについて、簡単な質問があります。具体的には、ループの繰り返しごとに評価するのではなく、for()ループの条件付き部分に現れる関数を評価するだけで十分ですか?Javaコントロール構造の自動コンパイラ最適化?
たとえば、次のコードを考えます。
// Array of doubles to "hold" the array.
private double matrix[][];
public int getCols() {
// Compute the number of columns in a matrix.
}
public int getRows() {
// Compute the number of rows in a matrix.
}
// Compute the sum of all elements in the matrix.
public double sum() {
double result = 0;
for (int r = 0; r < getRows(); r++) {
for (int c = 0; c < getCols(); c++) {
result += this.matrix[r][c];
}
}
return result;
}
明らかに、I()は
public double sum() {
double result = 0;
int numRows = getRows();
int numCols = getCols();
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
result += this.matrix[r][c];
}
}
return result;
}
にそれを変更することにより、ループの各反復で評価されていないの和()メソッドはそのGETROWS()とgetColsを確実に修正することができしかし、コンパイラがこれらを事前評価するのに十分なほどスマートであれば、私は不思議です。つまり、各繰り返しで評価するのではなく、事前に条件に表示されている関数を評価するのが計算上安価であることが自動的に検出されますか?
ありがとうございます!
状態の依存関係が極端に複雑になるとは思っても、配列の長さプロパティを使用していないのはなぜですか?現在の設定は、すべての行が同じ数の列を持つことを暗黙的に前提としているため、少し脆いです。 – Taylor