0

I have a two-dimensional array, object[,] mData

I need to retrieve the index of a specific string (key) in that object array without using a loop (e.g foreach, for).

The reason why I don't want to use a loop is because I am trying to optimize a current block of code that uses a loop which causes the process to take a long time since it is managing too much data.

Is there a way to do this?

CODE

`

object [,] mData = null;  
string sKey = String.Empty;  

for (int iIndex = 0; iIndex < mData.GetUpperBound(1); iIndex++)  
{  
    if (mData[0, iIndex].Value == sKey);  
    {
       return;  
    }  
}

`

1 Answers1

0

You will need a loop to linear search for an element. Even if there is a method that you can call to get the index (which there isn't, I don't think), there would still be a loop inside the method.

If you're worried about performance, try a binary search if the data is sorted.

I am trying to optimize a current block of code that uses a loop which causes the process to take a long time since it is managing too many data.

Loops don't neccessarily make your code run significantly slower. The core of your problem is that you have too many data. If you have that many data, then slowness is expected. What you can do is to run the time-consuming operation asynchronously so that the UI doesn't freeze.

Sweeper
  • 213,210
  • 22
  • 193
  • 313