2017-04-01 12 views
0

Ich möchte meine Insertion sortieren. Wenn ich den Code wie folgt es funktioniert, aber es druckt einmal für jede Art in der Schleife (weil es in der Schleife ist):Warum gibt die C++ Uhr unterschiedliche Werte zurück?

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'; 

Aber wenn ich die cout innerhalb der Schleife Kommentar, gibt es 0 Zeit genommen :

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'; 

Warum?

+2

Optimierung. Sortiert Array nach Schleife, um dies zu verhindern. –

+0

Ich würde wetten, weil Ihre Schleife in viel weniger Zeit ausgeführt wird, als es dauert, um die Zeit zu erfassen und vor allem an den Stream zu senden, der mit der Konsole oder einer Datei verbunden ist. Und normalerweise ist die Granularität der Uhr nicht so gut, um ein paar hundert Iterationen einer einfachen Schleife zu erfassen. –

+0

@yurikilochek Ja, das könnte das sein, was ich tun muss. – cparks10

Antwort

0

enter image description here

Todays CPUs sind zu schnell.

Diese folgende Sortierung verwendet Sleep(rand() % 100), um die Zeit zum Simulieren der Arbeitslast zu verzögern.

// 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; 
} 
Verwandte Themen