I am trying to get the running time of Insertion Sort Algorithm. MSDN said that using CTime could get the Elapsed Time. But I tried many times and always got zero. I thought it is impossible that the time of running this algorithm is zero. There must be some error or something else. Could anybody help me? I posted my code below:
#include <cstdlib>
#include <iostream>
#include <atltime.h>
using namespace std;
//member function
void insertion_sort(int arr[], int length);
int *create_array(int arrSize);
int main() {
//Create random array
int arraySize=100;
int *randomArray=new int[arraySize];
int s;
for (s=0;s<arraySize;s++){
randomArray[s]=(rand()%99)+1;
}
CTime startTime = CTime::GetCurrentTime();
int iter;
for (iter=0;iter<1000;iter++){
insertion_sort(randomArray,arraySize);
}
CTime endTime = CTime::GetCurrentTime();
CTimeSpan elapsedTime = endTime - startTime;
double nTMSeconds = elapsedTime.GetTotalSeconds()*1000;
cout<<nTMSeconds;
return 0;
}//end of main
CTime
has a resolution of one second. Apparently, your whole test takes less than a second. – Norikonorina