在一段时间后停止一个功能并转到下一个功能

时间:2016-11-17 22:10:28

标签: c++ time clock

我正在尝试测试一些算法并在计时时,如果它们花费太长时间(确切地说是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()函数总是需要很长时间,因为它无效且递归,这很好,它应该是这样的。但是,我不知道如何终止该特定功能,如果花费的时间太长,则移动到下一个功能。对不起罗嗦的帖子感到抱歉。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

让我们假设您在大小为n的输入数组上运行此算法。你的算法会触发两个递归调用,每个调用都运行在一个大小为n-1的数组上,然后执行一定量的工作来将这些部分组合在一起。这意味着我们可以将算法的运行时间表示为

  

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

此递归关系求解为O(2 n ),指数输入的大小。如果你测量一些输入需要多长时间,你应该能够从那里向外推断,知道你正在看一个指数增长曲线。具体来说,每个添加的元素都会使运行时倍增从那里,您只需要设置一个涉及2 n ,一分钟的方程式,以及算法在某些已知输入大小上的运行时间并从中获取。