Tag: 无与伦比

C#:端点为空(无穷大)时的范围交集

好的,我有这些交集方法来处理范围,只要范围端点不为null,它们就可以正常工作: public static bool Intersects(this Range first, Range second, IComparer comparer) { return comparer.Compare(first.Start, second.End) = 0; } public static Range GetIntersectionWith(this Range first, Range second, IComparer comparer) { // Return null, if any range is null or if they don’t intersect at all if (first == null || second == null || !Intersects(first, second, […]

为什么C#Array.BinarySearch如此之快?

我在C#中实现了一个非常简单的 binarySearch实现,用于在整数数组中查找整数: 二进制搜索 static int binarySearch(int[] arr, int i) { int low = 0, high = arr.Length – 1, mid; while (low <= high) { mid = (low + high) / 2; if (i arr[mid]) low = mid + 1; else return mid; } return -1; } 将它与C#的原生Array.BinarySearch()进行比较时,我发现Array.BinarySearch() 速度是我的函数的两倍多 。 Array.BinarySearch上的 MSDN: 使用由Array的每个元素和指定对象实现的IComparable通用接口,搜索特定元素的整个一维排序数组。 是什么让这种方法如此之快? 测试代码 […]