0

I am wanting to time my insertion sort. If I have the code like this it works, but it prints once for every sort in the loop (because it's in the loop):

clock_t start;
double duration;
start = clock();

int j, temp;

for (int i = 0; i < NumberOfLines; i++) {
    j = i;
    while (j > 0 && arr[j - 1] < arr[j]) {
        temp = arr[j];
        arr[j] = arr[j - 1];
        arr[j - 1] = temp;
        j--;
    }
    duration = (clock() - start) / (double)CLOCKS_PER_SEC;
    cout<<"Sorting took: "<< duration<<" seconds"<<'\n';
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
cout << "Please Work: " << duration << " seconds" << '\n';

But If I comment the cout inside the loop, it returns 0 time taken:

    duration = (clock() - start) / (double)CLOCKS_PER_SEC;
    //cout<<"Sorting took: "<< duration<<" seconds"<<'\n';
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
cout << "Please Work: " << duration << " seconds" << '\n';

Why?

cparks10
  • 377
  • 2
  • 14
  • 2
    Optimization. Print sorted array after loop to prevent it. – yuri kilochek Apr 01 '17 at 17:42
  • I'd bet because your loop is executed in far less time than it takes to catch the time and above all to emit to the stream that is connected to the console or a file. And usually the granularity of the clock is not so fine to catch a few hundred iterations of a simple loop. – Laurent G Apr 01 '17 at 17:45
  • @yurikilochek Yeah that might be what I have to do. – cparks10 Apr 01 '17 at 17:57
  • @LaurentG I'm not sure, because if I print the time in the loop (first code chunk above) it prints the time. – cparks10 Apr 01 '17 at 17:57
  • [This question](http://stackoverflow.com/questions/37786547/enforcing-statement-order-in-c/37789799) might be related. – Jeremy Apr 03 '17 at 14:39

1 Answers1

0

enter image description here

Todays CPU's are too fast.

This sort code below uses Sleep( rand() % 100 ) to delay time to simulate work load.

// sort program
// uses Sleep( rand() % 100 ) to delay time to simulate work load.
//

#include <time.h>
#include <iostream>
#include <windows.h>
using namespace std;

int main(  ){

    clock_t start=0, stop=0,start2=0,stop2=0;
    double duration=0,duration2=0, totalDurations=0.0;
    int i=0,j=0, temp=0;
    int NumberOfLines = 10;
    int arr[2001] = { 0 };

    cout<<"using Sleep( rand() % 100 ) to delay time to simulate work load. \n\n";

    // create random seed
    srand(time(0));

    // fill arr[  ] with random numbers
    for (int r = 0; r < NumberOfLines; r++){
        arr[r] = rand() % 1234;
    }

    // start total sort clock
    start = clock();

    //  begin sorting loop
    for ( i = 0; i < NumberOfLines; i++) {

        j = i;

         // start single item sort clock
         start2 = clock();
         Sleep( rand() % 100 );
         //cout << "(" << arr[i] << " ";

        // do actual single item sorting
        while ( (j > 0) && (arr[j - 1] < arr[j]) ) {
            temp = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = temp;
            j--;
        }

         // stop single item sort clock
         stop2 = clock();

         // calculate single item sort   time
         duration2 = (stop2 - start2) / (double)CLOCKS_PER_SEC;
         totalDurations += duration2;

         // display Single item sorting results
         cout << "Single item sorting took: " << duration2 << " seconds" << '\n';
        //cout << " " << arr[i] << ") ,";

    }

    // stop total sort clock
    stop = clock();

    // calculate total   sort   time
    duration = (stop - start) / (double)CLOCKS_PER_SEC;

    // display total  sorting results
    cout << '\n';
    cout << "Total Sorting took: " << duration << " seconds" << " (td.add+= ";
    cout<<" "<< totalDurations <<", error margin +-) \n";

    cout << " \nPress any key to continue\n";
    cin.ignore();
    cin.get();

    return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28