0

I;m using Lucene/.NET to implement a numerical search engine.

I want to filter numbers from within a large range, depends on which number exists in string array.

I used the following code:

int startValue = 1; 
endValue = 100000;

//Assume that the following string array contains 12000 strings
String[] ArrayOfTerms = new String[] { "1", "10",................. , "99995"};

public String[] GetFilteredStrings(String[] ArrayOfTerms)
{
  List<String> filteredStrings = new List<String>();

  for (int i = startValue; i <= endValue; i++)
  {                       
     int index = Array.IndexOf(ArrayOfTerms,i.ToString());

     if( index != -1)
     {
        filteredStrings.Add((String)ArrayOfTerms.GetValue(index));
     }      
  }

return filteredStrings.ToArray();

}

Now, my problem is it searches every value from 1 to 100000 and takes too much time. some times my application is hanging.

Can anyone of you help me how to improve this performance issue? I don't know about caching concept, but I know that Lucene supports cache filters. Should I use a cache filter? Thanks in advance.

4

2 に答える 2

0

実際、配列にアイテムが含まれているかどうかを判断しようとしています。HashSet や Dictionary などを使用して、O(n) 時間ではなく O(1) 時間の値の存在を判断できるようにする必要があると思います。

于 2012-07-20T10:14:54.073 に答える
0

このコードはかなり高速に動作します。

var results = ArrayOfTerms.Where(s => int.Parse(s) <= endValue);

やりたいことが手に入ったら

于 2012-07-20T10:22:30.740 に答える