gpt4 book ai didi

arrays - 使用LINQ可以改善我的代码吗?

转载 作者:行者123 更新时间:2023-12-04 19:01:54 25 4
gpt4 key购买 nike

我有这段代码,可以正常工作,但是在大型数据集上运行缓慢。

我想听听专家的意见,该代码是否可以从使用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

关于arrays - 使用LINQ可以改善我的代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com