在一段时间后停止一个函数并进入下一个函数

stopping a function after a set amount of time and going to the next function

本文关键字:函数 一个 下一个 一段时间      更新时间:2023-10-16

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

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

T (n)勒;2T(n - 1) + O(1)

这个递归关系解为O(2n),输入的大小呈指数级。如果你测量几个输入所花费的时间,你应该能够从那里向外推断,知道你在看一个指数增长曲线。具体来说,每个添加的元素都会使运行时间翻倍。从这里,您只需要建立一个等式,涉及2n, 1分钟和算法在某些已知输入大小上的运行时间,并从那里取东西。

相关文章: