title: Calculating Running Time of Binary Search
body: <p>The following Binary Search program is returning a running time of <code>0</code> milliseconds using <code>GetTickCount()</code> no matter how big the search item is set in the given list of values. </p>
  
  <p>Is there any other way to get the running time for comparison?</p>
  
  <p>Here's the code :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  using namespace std;
  
  int main(int argc, char **argv)
  {
          long int i = 1, max = 10000000;
          long int *data = new long int[max];
          long int initial = 1;
          long int final = max, mid, loc = -5;
          for(i = 1; i&lt;=max; i++)
          {
              data[i] = i;
          }
  
          int range = final - initial + 1;
          long int search_item = 8800000;
  
          cout&lt;&lt;"Search Item :- "&lt;&lt;search_item&lt;&lt;"\n";
  
          cout&lt;&lt;"-------------------Binary Search-------------------\n";
          long int start = GetTickCount();
          cout&lt;&lt;"Start Time : "&lt;&lt;start&lt;&lt;"\n";
  
          while(initial&lt;=final)
          {
              mid=(initial+final)/2;
  
              if(data[mid]==search_item)
              {
                  loc=mid;
                  break;
              }
  
              if(search_item&lt;data[mid])
                  final=mid-1;
  
              if(search_item&gt;data[mid])
                  initial=mid+1;
          }
          long int end = GetTickCount();
          cout&lt;&lt;"End Time : "&lt;&lt;end&lt;&lt;"\n";
          cout &lt;&lt; "time: " &lt;&lt; double(end - start)&lt;&lt;" milliseconds \n";
          if(loc==-5)
              cout&lt;&lt;" Required number not found "&lt;&lt;endl;
          else
              cout&lt;&lt;" Required number is found at index "&lt;&lt;loc&lt;&lt;endl;
          return 0;   
  }
  </code></pre>