c# - 二元搜索和indexof 哪个更快?

标签 c# performance arraylist binary-search indexof

我在 C# 中有一个非常大的字符串 ArrayList,并且定期在该 ArrayList 中搜索字符串。使用 ArrayList.IndexOf() 或 ArrayList.BinarySearch() 哪个更快?我可以对ArrayList进行排序。

最佳答案

该文档为您解释了一切。

来自ArrayList.BinarySearch :

The elements of the ArrayList must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.

This method is an O(log n) operation, where n is Count.

来自ArrayList.IndexOf

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

关于c# - 二元搜索和indexof 哪个更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34969585/

相关文章:

c# - 在 C# 中将一个 ulong 解析为两个 uint 变量

java - 显示多个字符串项目Java

java - 从 ar2[][][] 中删除 ar1[][]

c# - 将自定义文本添加到 MVC 5 的下拉列表

c# - 如何取消WPF窗体的最小化事件

c# - MySqlCommand.ExecuteNonQuery 失败

javascript - 服务器端处理与客户端处理+ ajax?

c# - 在单独的线程中延迟编译 .NET 正则表达式

android - 通过 Intent 传递 ArrayList

c# - 用于格式化 C# 代码的 VS2010 扩展