同时检查字符串是否在切片中?

huangapple go评论70阅读模式
英文:

Concurrently check if string is in slice?

问题

通常,为了检查一个字符串是否在切片中,我会编写一个带有for循环和if语句的函数。但是在处理大型字符串或结构类型的切片时,这种方法效率很低。是否有可能并发地进行这个检查呢?

英文:

Generally to check if a string is in slice I write a function with for loop and if statement. but it's really inefficient in cases of large slices of string or struct types. is it possible to make this check concurrent?

答案1

得分: 3

并发搜索顺序数据通常不是一个好主意,因为我们已经有了一个在数十亿条记录上都能很好扩展的二分搜索算法。你只需要在要搜索的切片上构建索引即可利用它。要构建最简单的索引,你需要将键保存到另一个切片中,并与它们所指向的数据的索引一起保存。一旦你有了这个切片,只需按字符串对其进行排序,索引就建立好了。

为了提高效率,你需要在刚刚创建的索引上执行二分搜索。这样你就可以达到O(log N)的复杂度。

另一个更简单的选择是创建map[string]int,并将所有键和索引插入其中。然后在map中查找索引,这种情况下的最佳情况复杂度为O(1)。

需要注意的重要一点是,如果你只需要在给定的切片上执行一次搜索,那么这种方法并不值得,因为创建索引比线性搜索要重。

英文:

The concurrent search on sequential data is usually not a great idea, simply because we already have a binary search that scales really well for even billions of records. All you have to do to utilize it is build indexing on top of the slice you are searching in. To build the most trivial indexing, you have to save keys into another slice along with the index of data they are pointing to. Once you have the slice, just sort it by strings, and indexing is done.

You have to perform the binary search on the indexing you just created to be more efficient. This way you have the complexity of O(log N).

Another much simpler option you have is creating the map[string]int and inserting all keys along with the indexes. Then find the index inside the map. Which can be O(1) best case.

The important thing to note is that if you have to perform just one search on a given slice, this is not worth it as creating indexing is a lot heavier than linear search.

huangapple
  • 本文由 发表于 2022年4月9日 20:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/71807959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定