Hi, I need to time sorting algorithms that I wrote, I wonder if there is better way to go more accurate like 2 decimal places, this is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <ctime>
#include <iostream>
usingnamespace std;
int main()
{
clock_t startTime = clock();
int arraySize = 100000;
int *insertionList = createArray(arraySize);
//sort this list
insertionSort(insertionList, arraySize);//my insertion sort fcn
clock_t endTime = clock();
clock_t duration = (endTime - startTime)/CLOCKS_PER_SEC;
cout<<"Elapsed time: "<<duration*1000<<" ms"<<endl;
return 0;
}
It's QueryPerformanceFrequency and QueryPerformanceCounter.
On Unices, there's clock_gettime() for high resolution measurements.
However, if milliseconds are not sufficient, then you're usually better off repeating your function for a few thousand/million times, which usually leads to far more accurate results.