ループ内の条件の最適化:C#の:私はネストされたループの下に持って
(...)
while (some_condition)
{
(...)
MyObject p = new MyObject(i, j);
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
// check matrix bounds
if (p.y + r <= 0 || p.y + r >= bound1 ||
p.x + c <= 0 || p.x + c >= bound2)
{
continue;
}
else if (matrix[p.y + r][p.x + c]=='$') // at this point no IndexOutOfBounds may be raised as it is checked in previous condition
{
continue;
}
AddItem(r, c);
}
}
}
MyObjectには、とりわけattirbutes下回っていたクラスです。
public class MyObject {
public int x;
public int y;
public MyObject(int x, int y)
{
this.x = x;
this.y = y;
}
// Other methods ....
}
だから私は私が意味する、パフォーマンスが心配ですパフォーマンスが低下する可能性があるので、条件付きループ内が好きではないので、どのように最適化できますか?
また、私は、コードを読みやすくしたいので、私は以下のようにそれを書き換えています
while (some_condition)
{
(...)
MyObject p = new MyObject(i, j);
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
if (!IsOutOfBounds(r, c, p) && !IsDollar(r, c, p))
{
AddItem(r, c);
}
}
}
}
private bool IsOutOfBounds(int r, int c, MyObject p)
{
return (p.y + r <= 0 || p.y + r >= bound1 ||
p.x + c <= 0 || p.x + c >= bound2);
}
private bool IsDollar(int r, int c, MyObject p)
{
// matrix is global
return (matrix[p.y + r][p.x + c]=='$');
}
しかし、今、ループ内の呼び出し機能もパフォーマンスが低下するので、どのようにして、関数をインライン化? [MethodImpl(MethodImplOptions.AggressiveInlining)]属性の両方の関数を先に実行する必要がありますか?
私はコードレビューサイトはあなたのために良いと思う。 –
[ヘルプセンター](http://codereview.stackexchange.com/help)を最初にチェックアウトすることをお勧めします。 – Incomputable
_「パフォーマンスが低下する可能性があるため、ループ内の条件が嫌いです」_ただし、慰めとして、例外が保存されます。 –