我的代码可以改进使用LINQ吗?

Yea*_*son 5 linq vb.net arrays

我有这个代码,它工作正常,但在大型数据集上运行缓慢.

我想听听专家的意见,如果这个代码可以从使用Linq或其他方法中受益,如果是这样,怎么样?

  Dim array_of_strings As String()

  ' now I add strings to my array, these come from external file(s). 
  ' This does not take long

 ' Throughout the execution of my program, I need to validate millions
 ' of other strings.

  Dim search_string As String
  Dim indx As Integer

  ' So we get million of situation like this, where I need to find out
 ' where in the array I can find a duplicate of this exact string

  search_string = "the_string_search_for"

  indx = array_of_strings.ToList().IndexOf(search_string)
Run Code Online (Sandbox Code Playgroud)

我的数组中的每个字符串都是唯一的,没有重复.

这很好用,但就像我说的那样,对于较大的数据集来说太慢了.我运行这个查询数百万次.目前,一百万次查询大约需要1分钟,但这对我来说太慢了.

Mic*_*urn 5

没有必要使用Linq.如果您使用索引数据结构(如字典),则搜索将为O(log n),代价是稍微长一点的填充结构.但是你做了一次,然后做了一百万次搜索,你就会提前出来.

请参阅此站点上的词典说明:https: //msdn.microsoft.com/en-us/library/7y3x785f(v = vs.110).aspx

因为(我认为)你在谈论一个自己的密钥集合,你可以使用https://msdn.microsoft.com/en-us/library/dd412070(v=vs.110)来节省一些内存SortedSet<T> . ASPX