Zeitmessung
-
Hallo,
ich bin Programmieranfänger uns möchte nun die Laufzeit einiger Sortierverfahren vergleichen. Was muss ich dazu tun?
Ich benutze DEV C-PluPlus zum Programmieren.
-
clock() aus ctime
-
// Zeitmessung #include <iostream> #include <ctime> // clock_t, clock(), ... #include <conio.h> #include <cmath> using namespace std; int main() { const int NMAX = 20; int max; cout << "Anzahl Schleifendurchlaeufe? "; cin >> max; clock_t t1,t2; double ts, tm=0; double a; cout << endl; for(int n=0; n<NMAX; ++n) { t1 = clock(); // Start for( int i=0; i<max;i++) { a = sin(a); //zu messende Aktion 1 } t2 = clock(); // Ende ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); // Zeit in Sekunden. cout << "Zeit Aktion 1: " << ts << " sec" << endl; tm += ts; // Das ist das Gleiche wie tm = tm + ts } tm/=NMAX; // Das ist das Gleiche wie tm = tm / NMAX cout << "Durchschnitts-Zeit bei Aktion 1: " << tm << " sec" << endl; tm=0; cout << endl; for(int n=0; n<NMAX; ++n) { t1 = clock(); for( int i=0; i<max;i++) { a = cos(a); //zu messende Aktion 2 } t2 = clock(); ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); cout << "Zeit bei Aktion 2: " << ts << " sec" << endl; tm += ts; } tm/=NMAX; cout << "Durchschnitts-Zeit bei Aktion 2: " << tm << " sec" << endl; getch(); }