2016-11-17 61 views
-4

我試圖測試一些算法,並計時它們,如果它們花費太長時間(準確地說60秒),我想停止它們。我已經試着擺弄時鐘功能,但我似乎無法讓它停下來,並轉向下一個測試。我想這樣做而不編輯isUnique函數本身。有沒有辦法通過從開始計時操作並在經過60秒後停止操作?以下是節目至今..在設定的時間量後停止功能並轉到下一個功能

#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; 
    double timeout = 60.0; 

    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, m); 
    if (isUnique(myVect, 0, m) == 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; 

第一isUnique設置()函數總是需要很長的,因爲它的無效和遞歸,這很好,它應該是這樣的。但是,我不知道如何終止這個特定的功能,並且如果它需要很長時間才能轉到下一個功能。對羅嗦的帖子感到抱歉。有什麼建議麼?

+0

是什麼問題? – amit

+0

如何使用該算法找到n的最大值,以便算法在一分鐘或更短時間內運行? –

+0

(1)嘗試'n'的各種值。 (2)「運行一分鐘或更少」是有點機器(和編譯器)具體... – amit

回答

2

讓我們假設你在一個大小爲n的輸入數組上運行這個算法。你的算法觸發兩個遞歸調用,每個調用都運行在一個大小爲n-1的數組上,然後進行持續的工作以將這些碎片重新組合在一起。這意味着,我們可以表達你的算法的運行時間爲

T(n)的≤ 2T(N - 1)+ O(1)

這種遞推關係解決到O(2 ñ),指數大小的輸入。如果您測量一些輸入需要多長時間,那麼您應該能夠從那裏向外推斷,知道您正在尋找指數增長曲線。具體而言,每個添加的元素將使運行時間加倍從那裏開始,你只需要建立一個包含2 n的公式,一分鐘,以及算法在某些已知輸入大小上的運行時間,並從那裏獲取信息。

+0

你對我說希臘語。我根本不知道你在說什麼男人。我是這個新東西 –

+0

你的函數進行兩次遞歸調用,每次調用都在一個數組上,它比原始數組有效一步。所以這意味着你對一個大小爲n的數組進行遞歸調用,對n-1大小的數組有兩個,對大小爲n-2的數組有四個,對大小爲n-3的數組有八個,等等。導致出現很多遞歸調用,事實上,它們是指數級的大量調用。每次您將問題的大小增加1時,您都會像以前那樣啓動兩次遞歸調用,這意味着該功能需要大約兩倍的時間才能完成。 – templatetypedef

+0

謝謝,這是有道理的。儘管如此,我仍然不明白我該如何解決這一切。這只是我必須測試的三本算法之一。我希望可以用一組代碼來幫忙,我將這些代碼輸入到isUnique()函數中,我可以使用其他兩個函數來測試。 –