2016-11-24 2 views
0

Ich teste einige Algorithmen und Timing sie. Ich würde gerne wissen, wie man die Funktion bei laufendem Betrieb abbricht, wenn sie länger als 60 Sekunden läuft. Hier ist, was ich arbeite mit:Wie kann ich eine Funktion nach Ablauf der Zeit abbrechen?

#include "stdafx.h" 
#include <iostream> 
#include <vector> 
#include <ctime> 
#include <chrono> 
#include <cstdlib> 
#include <random> 
#include <algorithm> 

using namespace std; 

bool isUnique(const vector<int>& arr, int start, int end) { 
    if (start >= end) return true; 
    if (!isUnique(arr, start, end - 1)) 
     return false; 
    if (!isUnique(arr, start + 1, end)) 
     return false; 
    return (arr[start] != arr[end]); 
} 

bool isUniqueLoop(const vector<int>& arr, int start, int end) { 
    if (start >= end) return true; 
    for (int i = start; i < end; i++) 
     for (int j = i + 1; j <= end; j++) 
      if (arr[i] == arr[j])return false; 
    return true; 
} 

bool isUniqueSort(const vector<int>& arr, int start, int end) { 
    if (start <= end) return true; 
    vector<int> buf(arr); 
    sort(buf.begin() + start, buf.begin() + end); 
    for (int i = start; i < end; i++) 
     if (buf[i] == buf[i + 1]) return false; 
    return true; 
} 

int main() { 

     int max = 0; 
     cout << "Enter a number for the Max range: "; 
     cin >> max; 
     default_random_engine randGen(time(0)); 
     uniform_int_distribution<int> randNum(0, max); 
     int i; 
     int j; 
     int n = randNum(randGen); 
     int m = n; 

     vector<int> myVect; 

     for (i = 0; i <= m; i++) { 
      myVect.push_back(randNum(randGen)); 
      //cout << myVect[i] << endl; 
     } 
     cout << "Recursive Algorithm Test... " << endl; 
     cout << endl; 

     // recursive algorithm 
      clock_t start = clock(); 
     isUnique(myVect, 0, n); 
      if (isUnique(myVect, 0, n) == true) { 
      cout << "The Vector is Unique! " << endl; 
     } 
     else { 
      cout << "The Vector is not Unique! " << endl; 
     } 
     clock_t end = clock(); 
     double time = (double)(end - start)/CLOCKS_PER_SEC * 1000.0; 
     cout << "CPU Time used for this algorithm: " << time << " ms" << endl; 

     if (time > 60000) { 
     cout << "This function takes too long! " << endl; 
       } 

     cout << "------------------------------------" << endl; 


     cout << "Iterative Algorithm Test... " << endl; 
     cout << endl; 
     // iterative algorithm 
     clock_t start2 = clock(); 
     isUniqueLoop(myVect, 0, n); 
     if (isUniqueLoop(myVect, 0, n) == true) { 
      cout << "The Vector is Unique! " << endl; 
     } 
     else { 
      cout << "The Vector is not Unique! " << endl; 
     } 
     clock_t end2 = clock(); 
     double time2 = (double)(end2 - start2)/CLOCKS_PER_SEC * 1000.0; 
     cout << "CPU time used for this algorithm: " << time2 << " ms. " << endl; 
     if (time2 > 60000) { 
      cout << "This function takes too long! " << endl; 
     } 
     cout << "------------------------------------" << endl; 


     cout << "Sort Algorithm Test... " << endl; 
     cout << endl; 
     // sort algorithm 
     clock_t start3 = clock(); 
     isUniqueSort(myVect, 0, n); 
     if (isUniqueSort(myVect, 0, n) == true) { 
      cout << "The Vector is Unique! " << endl; 
     } 
     else { 
      cout << "The Vector is not Unique " << endl; 
     } 
     clock_t end3 = clock(); 
     double time3 = (double)(end3 - start3)/CLOCKS_PER_SEC * 1000.0; 
     cout << "CPU time used for this algorithm: " << time3 << " ms. " << endl; 
     if (time3 > 60000) { 
      cout << "This function takes too long! " << endl; 
     } 
     cout << endl; 
     system("pause"); 
     return 0; 
} 

alles funktioniert, außer der Tatsache, dass ich es die Funktion isUnique (myVect, 0, m) beenden wollen, wenn sie länger als 60 Sekunden dauert .... Irgendwelche Vorschläge ?

+0

Wenn Sie es in einem anderen Thread ausgeführt wird, dann können Sie den Thread töten, aber wenn es im selben Thread dann die Funktion zu ermitteln hat, wie lange sie den Ausgang selbst in Betrieb war nach dem erforderlichen Timeout. –

+0

Ich habe noch nichts über Threads gelernt, also bin ich mir nicht sicher, wovon du sprichst. Ich weiß, dass die Funktion erkennen muss, wie lange sie läuft, aber ich weiß nicht, wie ich das machen soll. –

Antwort

0

Wenn Sie außergewöhnliche Umstände (wie eine Zeitüberschreitung) haben, können Sie eine Ausnahme auslösen. In Ihrem Fall, welche Art von Ausnahmen liegt an Ihnen. Ausnahmen funktionieren gut, wenn Sie eine Aufrufstruktur mit vielen Ebenen beenden möchten, ohne jede Funktion einzeln auf einen Beendigungscode überprüfen zu müssen. Im Beispiel unten ich eine Zeichenfolge als eine Ausnahme bin werfen:

bearbeiten

Um hier oft tun, um die Zeitberechnungen Art und Weise zu verhindern, ist ein Versuch, zu begrenzen, wie oft sie fertig sind. Dies ist keineswegs wissenschaftlich, aber auf meiner Box gibt es ein vernünftiges Ergebnis. Wenn Sie einen besseren Weg haben, lassen Sie es mich wissen.

#include <iostream> 
#include <vector> 
#include <ctime> 
#include <chrono> 
#include <cstdlib> 
#include <random> 
#include <algorithm> 

bool isUnique(const std::vector<int>& arr, int start, int end, clock_t clock_start, 
       unsigned long & iterations, unsigned long timecheck) { 

    if (++iterations > timecheck) { 
     iterations = 0; 
     clock_t clock_current = clock(); 
     double time = (double)(clock_current - clock_start) * 1000.0/CLOCKS_PER_SEC; 
     if (time > 60000) throw "This function takes too long!"; 
     else std::cout << time << " ms elapsed..." << std::endl; 
    } 

    if (start >= end) return true; 
    if (!isUnique(arr, start, end - 1, clock_start, iterations, timecheck)) 
     return false; 
    if (!isUnique(arr, start + 1, end, clock_start, iterations, timecheck)) 
     return false; 
    return (arr[start] != arr[end]); 
} 

bool isUniqueLoop(const std::vector<int>& arr, int start, int end, clock_t clock_start, 
        unsigned long & iterations, unsigned long timecheck) { 

    if (start >= end) return true; 
    for (int i = start; i < end; i++) { 
     for (int j = i + 1; j <= end; j++) { 

      if (++iterations > timecheck) { 
       iterations = 0; 
       clock_t clock_current = clock(); 
       double time = (double)(clock_current - clock_start) * 1000.0/CLOCKS_PER_SEC; 
       if (time > 60000) throw "This function takes too long!"; 
       else std::cout << time << " ms elapsed..." << std::endl; 
      } 

      if (arr[i] == arr[j])return false; 
     } 
    } 
    return true; 
} 

bool isUniqueSort(const std::vector<int>& arr, int start, int end, clock_t clock_start, 
        unsigned long & iterations, unsigned long timecheck) { 

    if (start <= end) return true; 
    std::vector<int> buf(arr); 
    sort(buf.begin() + start, buf.begin() + end); 
    for (int i = start; i < end; i++) { 

     if (++iterations > timecheck) { 
      iterations = 0; 
      clock_t clock_current = clock(); 
      double time = (double)(clock_current - clock_start) * 1000.0/CLOCKS_PER_SEC; 
      if (time > 60000) throw "This function takes too long!"; 
      else std::cout << time << " ms elapsed..." << std::endl; 
     } 

     if (buf[i] == buf[i + 1]) return false; 
    } 
    return true; 
} 

unsigned long calculate_timecheck() { 
    // since we want to limit the functions to 60 seconds we need to check how 
    // long they have been running but we don't want to do that all the time 
    // so we should only check so often - here is an attemopt to figure out how often 

    unsigned long upperlimit = 1; 
    volatile unsigned long i; 
    clock_t start, current; 
    double time; 

    do { 
     if (upperlimit > ULONG_MAX/2) return ULONG_MAX-1; 
     upperlimit *= 2; 
     start = clock(); 
     for (i = 0; i < upperlimit*100000; i++); 
     current = clock(); 
     time = (double)(current - start) * 1000.0/CLOCKS_PER_SEC; 
     std::cout << upperlimit*100000 << " iterations took " << time << " ms." << std::endl; 
    } while (time<500); 

    return upperlimit*100000; 
} 

int main() { 

    int max = 0; 
    std::cout << "Enter a number for the Max range: "; 
    std::cin >> max; 
    std::default_random_engine randGen(time(0)); 
    std::uniform_int_distribution<int> randNum(0, max); 
    int n = randNum(randGen); 
    int m = n; 

    std::vector<int> myVect; 

    for (int i = 0; i <= m; i++) { 
     myVect.push_back(randNum(randGen)); 
     //std::cout << myVect[i] << std::endl; 
    } 

    std::cout << "Calculating timeout... " << std::endl; 
    std::cout << std::endl; 

    unsigned long timecheck = calculate_timecheck(); 
    std::cout << "Will check for timeout every " << timecheck << " iterations..." << std::endl; 
    std::cout << std::endl; 

    std::cout << "------------------------------------" << std::endl; 

    // recursive algorithm 
    try { 
     std::cout << "Recursive Algorithm Test... " << std::endl; 
     std::cout << std::endl; 

     clock_t start = clock(); 
     unsigned long iterations = 0; 
     // isUnique(myVect, 0, n, start, iterations, timecheck); 
     if (isUnique(myVect, 0, n, start, iterations, timecheck) == true) { 
      std::cout << "The Vector is Unique! " << std::endl; 
     } 
     else { 
      std::cout << "The Vector is not Unique! " << std::endl; 
     } 

     clock_t end = clock(); 
     double time = (double)(end - start) * 1000.0/CLOCKS_PER_SEC; 
     std::cout << "CPU Time used for this algorithm: " << time << " ms." << std::endl; 
    } catch (const char *err) { 
     std::cout << err << std::endl; 
    } 

    std::cout << "------------------------------------" << std::endl; 

    // iterative algorithm 
    try { 
     std::cout << "Iterative Algorithm Test... " << std::endl; 
     std::cout << std::endl; 

     clock_t start = clock(); 
     unsigned long iterations = 0; 
     // isUniqueLoop(myVect, 0, n, start, iterations, timecheck); 
     if (isUniqueLoop(myVect, 0, n, start, iterations, timecheck) == true) { 
      std::cout << "The Vector is Unique! " << std::endl; 
     } 
     else { 
      std::cout << "The Vector is not Unique! " << std::endl; 
     } 

     clock_t end = clock(); 
     double time = (double)(end - start) * 1000.0/CLOCKS_PER_SEC; 
     std::cout << "CPU time used for this algorithm: " << time << " ms." << std::endl; 
    } catch (const char *err) { 
     std::cout << err << std::endl; 
    } 

    std::cout << "------------------------------------" << std::endl; 

    // sort algorithm 
    try { 
     std::cout << "Sort Algorithm Test... " << std::endl; 
     std::cout << std::endl; 

     clock_t start = clock(); 
     unsigned long iterations = 0; 
     // isUniqueSort(myVect, 0, n, start, iterations, timecheck); 
     if (isUniqueSort(myVect, 0, n, start, iterations, timecheck) == true) { 
      std::cout << "The Vector is Unique! " << std::endl; 
     } 
     else { 
      std::cout << "The Vector is not Unique " << std::endl; 
     } 

     clock_t end = clock(); 
     double time = (double)(end - start) * 1000.0/CLOCKS_PER_SEC; 
     std::cout << "CPU time used for this algorithm: " << time << " ms." << std::endl; 
    } catch (const char *err) { 
     std::cout << err << std::endl; 
    } 

    std::cout << std::endl; 
    system("pause"); 
    return 0; 
} 

Original:

#include <iostream> 
#include <vector> 
#include <ctime> 
#include <chrono> 
#include <cstdlib> 
#include <random> 
#include <algorithm> 

using namespace std; 

bool isUnique(const vector<int>& arr, int start, int end, clock_t clock_start) { 

    clock_t clock_current = clock(); 
    double time = (double)(clock_current - clock_start)/CLOCKS_PER_SEC * 1000.0; 
    if (time > 60000) throw "This function takes too long!"; 

    if (start >= end) return true; 
    if (!isUnique(arr, start, end - 1, clock_start)) 
     return false; 
    if (!isUnique(arr, start + 1, end, clock_start)) 
     return false; 
    return (arr[start] != arr[end]); 
} 

bool isUniqueLoop(const vector<int>& arr, int start, int end, clock_t clock_start) { 
    if (start >= end) return true; 
    for (int i = start; i < end; i++) { 
     for (int j = i + 1; j <= end; j++) { 

      clock_t clock_current = clock(); 
      double time = (double)(clock_current - clock_start)/CLOCKS_PER_SEC * 1000.0; 
      if (time > 60000) throw "This function takes too long!"; 

      if (arr[i] == arr[j])return false; 
     } 
    } 
    return true; 
} 

bool isUniqueSort(const vector<int>& arr, int start, int end, clock_t clock_start) { 
    if (start <= end) return true; 
    vector<int> buf(arr); 
    sort(buf.begin() + start, buf.begin() + end); 
    for (int i = start; i < end; i++) { 

     clock_t clock_current = clock(); 
     double time = (double)(clock_current - clock_start)/CLOCKS_PER_SEC * 1000.0; 
     if (time > 60000) throw "This function takes too long!"; 

     if (buf[i] == buf[i + 1]) return false; 
    } 
    return true; 
} 

int main() { 

    int max = 0; 
    cout << "Enter a number for the Max range: "; 
    cin >> max; 
    default_random_engine randGen(time(0)); 
    uniform_int_distribution<int> randNum(0, max); 
    int i; 
    int j; 
    int n = randNum(randGen); 
    int m = n; 

    vector<int> myVect; 

    for (i = 0; i <= m; i++) { 
     myVect.push_back(randNum(randGen)); 
     //cout << myVect[i] << endl; 
    } 

    try { 
     cout << "Recursive Algorithm Test... " << endl; 
     cout << endl; 

     // recursive algorithm 
     clock_t start = clock(); 
     isUnique(myVect, 0, n, start); 
     if (isUnique(myVect, 0, n, start) == true) { 
      cout << "The Vector is Unique! " << endl; 
     } 
     else { 
      cout << "The Vector is not Unique! " << endl; 
     } 

     clock_t end = clock(); 
     double time = (double)(end - start)/CLOCKS_PER_SEC * 1000.0; 
     cout << "CPU Time used for this algorithm: " << time << " ms" << endl; 
    } catch (const char *err) { 
     cout << err << endl; 
    } 

    cout << "------------------------------------" << endl; 

    try { 
     cout << "Iterative Algorithm Test... " << endl; 
     cout << endl; 

     // iterative algorithm 
     clock_t start2 = clock(); 
     isUniqueLoop(myVect, 0, n, start2); 
     if (isUniqueLoop(myVect, 0, n, start2) == true) { 
      cout << "The Vector is Unique! " << endl; 
     } 
     else { 
      cout << "The Vector is not Unique! " << endl; 
     } 

     clock_t end2 = clock(); 
     double time2 = (double)(end2 - start2)/CLOCKS_PER_SEC * 1000.0; 
     cout << "CPU time used for this algorithm: " << time2 << " ms. " << endl; 
    } catch (const char *err) { 
     cout << err << endl; 
    } 

    cout << "------------------------------------" << endl; 

    try { 
     cout << "Sort Algorithm Test... " << endl; 
     cout << endl; 

     // sort algorithm 
     clock_t start3 = clock(); 
     isUniqueSort(myVect, 0, n, start3); 
     if (isUniqueSort(myVect, 0, n, start3) == true) { 
      cout << "The Vector is Unique! " << endl; 
     } 
     else { 
      cout << "The Vector is not Unique " << endl; 
     } 

     clock_t end3 = clock(); 
     double time3 = (double)(end3 - start3)/CLOCKS_PER_SEC * 1000.0; 
     cout << "CPU time used for this algorithm: " << time3 << " ms. " << endl; 
    } catch (const char *err) { 
     cout << err << endl; 
    } 

    cout << endl; 
    system("pause"); 
    return 0; 
} 
+0

Ich sehe nicht wirklich, was du getan hast, außer das ganze Ding in einen Versuchsblock zu legen und eine Schnur zu benutzen. Ich versuche die Funktion nach 60 Sekunden zu stoppen. Ich muss wissen, wie man die isUnique-Funktion während des Laufens konstant einstellt und sie stoppt, wenn sie für 60 Sekunden oder länger ausgeführt wird. –

+0

Nevermind, ich habe nicht gesehen, wo Sie die tatsächlichen Funktionen Argumente und Körper bearbeitet haben. Ich werde es versuchen und sehen, was passiert .. kein Wortspiel beabsichtigt –

+0

@ElliotWerner Sie müssen die Startzeit übergeben, so dass Sie immer wissen, was es ist - aber die aktuelle Zeit muss in der Funktion berechnet werden. Sie können den Rückgabetyp in einen Wert ändern, der drei Werte enthalten kann und einen Rückgabewert "Zeitüberschreitung" zurückgibt, aber dann müssen Sie ihn überall überprüfen - aber dafür wurde eine Ausnahme entwickelt. –

Verwandte Themen