c++ - Calculating Running Time of Binary Search -


the following binary search program returning running time of 0 milliseconds using gettickcount() no matter how big search item set in given list of values.

is there other way running time comparison?

here's code :

#include <iostream> #include <windows.h> using namespace std;  int main(int argc, char **argv) {         long int = 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<=max; i++)         {             data[i] = i;         }          int range = final - initial + 1;         long int search_item = 8800000;          cout<<"search item :- "<<search_item<<"\n";          cout<<"-------------------binary search-------------------\n";         long int start = gettickcount();         cout<<"start time : "<<start<<"\n";          while(initial<=final)         {             mid=(initial+final)/2;              if(data[mid]==search_item)             {                 loc=mid;                 break;             }              if(search_item<data[mid])                 final=mid-1;              if(search_item>data[mid])                 initial=mid+1;         }         long int end = gettickcount();         cout<<"end time : "<<end<<"\n";         cout << "time: " << double(end - start)<<" milliseconds \n";         if(loc==-5)             cout<<" required number not found "<<endl;         else             cout<<" required number found @ index "<<loc<<endl;         return 0;    } 

your code looks this:

int main() {     // code...      while (some_condition)     {         // more code...         // print timing result         return 0;     } } 

that's why code prints 0 time, 1 iteration of loop exit program.


Comments