Reputation: 11
I know its a dumb question to ask about , why this code minX is computationally expensive, but i thought, someone might inform me of my mistake. Thanks
// getMap is a 2 dimentional array of type short I need to find maxX,minX,maxY,minY
//Can't use Math.Min , as it will get the lower bound i.e 0, i want values after filtering
for (int X = 0; X < getMap.GetLength(0); X++)
{
for (int Y = 0; Y < getMap.GetLength(1); Y++)
{
if((short)getMap.GetValue(X,Y) != 0)
{
//if i take it out, it works fine ,I need to
if (minX > X )
minX = X;
else
X = minX;
// Prints some stuff and does other computation
}
}
}
// Draws image
Upvotes: 0
Views: 246
Reputation: 273244
I don't know how (short)getMap.GetValue(X,Y)
compares to just getmap[X,Y]
but is seems over-complicated.
What you can do is to replace short[,] getMap
with short[][] getMap
.
Array-of-array (jagged arrays) are ususally faster in these nested loop scenarios because the Jitter can optimize range checking better.
Upvotes: 1
Reputation: 108790
the else X = minX;
part doesn't make much sense. It can cause an endless loop because you're setting back the loop variable X
to a lower value.
Your code will only terminate if at most 1 column contains a non zero value(assuming you initialized minX to a large value).
For example assume that both the X=0
and the X=1
column contain a non zero value somewhere. Then when it hits the non zero value in the 0 column minX
is set to 0. Then later it hits the non zero value with X=1
notices that minX>X
is false and sets X back to 0. And repeat forever.
Upvotes: 4