performance - Why is C# Array.BinarySearch so fast? -


i have implemented very simple binarysearch algorithm in c# finding integers in integer array:

binary search

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])             high = mid-1;          else if (i > arr[mid])             low = mid+1;          else return mid;     }     return -1; } 

when comparing c#'s native array.binarysearch() can see array.binarysearch() more twice fast function, every single time.

msdn on array.binarysearch:

searches entire one-dimensional sorted array specific element, using icomparable generic interface implemented each element of array , specified object.

what makes approach fast?

test code

using system; using system.diagnostics;  class program {     static void main()     {         random rnd = new random();         stopwatch sw = new stopwatch();          const int elements = 10000000;         int temp;          int[] arr = new int[elements];         (int = 0; < elements; i++)             arr[i] = rnd.next(int.minvalue,int.maxvalue);          array.sort(arr);          //custom binarysearch         sw.restart();         (int = 0; < elements; i++)             temp = binarysearch(arr, i);         sw.stop();         console.writeline($"elapsed time custom binarysearch: {sw.elapsedmilliseconds}ms");          //c# array.binarysearch         sw.restart();         (int = 0; < elements; i++)             temp = array.binarysearch(arr,i);         sw.stop();         console.writeline($"elapsed time c# binarysearch: {sw.elapsedmilliseconds}ms");     }      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])                 high = mid-1;              else if (i > arr[mid])                 low = mid+1;              else return mid;         }         return -1;     } } 

test results

+------------+--------------+--------------------+ | attempt no | binarysearch | array.binarysearch | +------------+--------------+--------------------+ |          1 | 2700ms       | 1099ms             | |          2 | 2696ms       | 1083ms             | |          3 | 2675ms       | 1077ms             | |          4 | 2690ms       | 1093ms             | |          5 | 2700ms       | 1086ms             | +------------+--------------+--------------------+ 

your code faster when run outside visual studio:

yours vs array's:

from vs - debug mode: 3248 vs 1113 vs - release mode: 2932 vs 1100 running exe - debug mode: 3152 vs 1104 running exe - release mode: 559 vs 1104 

array's code might optimized in framework lot more checking version (for instance, version may overflow if arr.length greater int.maxvalue / 2) and, said, designed wide range of types, not int[].

so, basically, it's slower when debugging code, because array's code run in release , less control behind scenes.


Comments