Why is C# Array.BinarySearch so fast?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:36:34

Your code is faster when run outside Visual Studio:

Yours vs Array's:

From VS - Debug mode: 3248 vs 1113
From VS - Release mode: 2932 vs 1100
Running exe - Debug mode: 3152 vs 1104
Running exe - Release mode: 559 vs 1104

Array's code might be already optimized in the framework but also does a lot more checking than your version (for instance, your version may overflow if arr.Length is greater than int.MaxValue / 2) and, as already said, is designed for a wide range of types, not just int[].

So, basically, it's slower only when you are debugging your code, because Array's code is always run in release and with less control behind the scenes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!