加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-17 00:10:53 所属栏目:大数据 来源:网络整理
导读:我有这个代码,它工作正常,但在大型数据集上运行缓慢. 我想听听专家的意见,如果这个代码可以从使用Linq或其他方法中受益,如果是这样,怎么样? Dim array_of_strings As String() ' now I add strings to my array,these come from external file(s). ' This d
我有这个代码,它工作正常,但在大型数据集上运行缓慢.

我想听听专家的意见,如果这个代码可以从使用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)

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

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读