I have code that generates random numbers from 1-100 and sorts them using the merge sort which I already have in a separate function. Everything works but when I implement clock(); to try and get the running time, I always get zero. I have even tried with larger numbers like 10000 but still, the time passed always gives me zero. here is my code
int main() { clock_t startTime; clock_t endTime; clock_t timePassed; int array[100]; srand(time(NULL)); int n = sizeof(array) / sizeof(array[0]); startTime = clock(); for (int j = 0; j < 100; j++) { array[j] = rand() % 100+1; std::cout << array[j] << " "; } std::cout << "\n"; MergeSort(array, n); std::cout << "After Merge Sort :" << std::endl; PrintArray(array, n); endTime = clock(); timePassed = ((endTime - startTime) / CLOCKS_PER_SEC); std::cout << "\n" << timePassed; } return 0; }
cout.precision(9999);float(timePassed)std::chronofaculties of the standard library will give you better and more accurate results.clock, you just are using it.clockis implemented inside your C++ or C standard library (on Linux, using clock_gettime(2) ...)