LINQ Performance for Large Collections

前端 未结 6 1811
梦毁少年i
梦毁少年i 2021-02-02 11:48

I have a large collection of strings (up to 1M) alphabetically sorted. I have experimented with LINQ queries against this collection using HashSet, SortedDictionary, and Dictio

6条回答
  •  天命终不由人
    2021-02-02 12:06

    I think the problem is that Linq has no way to use the fact that your sequence is already sorted. Especially it cannot know, that applying the StartsWith function retains the order.

    I would suggest to use the List.BinarySearch method together with a IComparer that does only comparison of the first query chars (this might be tricky, since it's not clear, if the query string will always be the first or the second parameter to ()).

    You could even use the standard string comparison, since BinarySearch returns a negative number which you can complement (using ~) in order to get the index of the first element that is larger than your query.

    You have then to start from the returned index (in both directions!) to find all elements matching your query string.

提交回复
热议问题