【问题标题】:stopping a function after a set amount of time and going to the next function [closed]在设定的时间后停止功能并转到下一个功能[关闭]
【发布时间】:2016-11-17 22:10:27
【问题描述】:

我正在尝试测试一些算法,并在计时时,如果它们花费的时间太长(准确地说是 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 的最大值,以使算法在一分钟或更短的时间内运行?
  • (1) 尝试n 的各种值。 (2)“运行一分钟或更少”是特定于机器(和编译器)的......
  • 60 秒,调用 isUnique() 计算调用了多少次?但是 arr 有多大?我建议你的第一个实验应该是-O0。当您的代码似乎可以工作时,更改为 -O3。输出是每 60 秒计数吗?
  • 我问的问题正是我的书问它的方式。再说一遍。老实说,我不知道该怎么做,我从头文件中谈论,向量需要如何设置,如何计时……没有。我完全不明白。

标签: c++ time clock


【解决方案1】:

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

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

这种递归关系求解为 O(2n),输入大小的指数。如果您测量几个输入需要多长时间,您应该能够从那里向外推断,因为您知道您正在查看指数增长曲线。具体来说,每个添加的元素都会使运行时间加倍。从那里,您只需要建立一个涉及 2n、一分钟和算法在某个已知输入大小上的运行时间的方程,然后从那里获取东西。

【讨论】:

  • 你在跟我说希腊语。我完全不知道你在说什么。我是这方面的新手
  • 您的函数进行了两次递归调用,每一次都位于一个实际上比原始数组小一步的数组上。所以这意味着你对一个大小为 n 的数组有一个递归调用,对一个大小为 n -1 的数组有两个递归调用,对一个大小为 n - 2 的数组有四个递归调用,对一个大小为 n - 3 的数组有八个等等。这结束了导致有很多递归调用,事实上,它们的数量呈指数级增长。每次将问题的大小增加 1 时,启动的递归调用次数是以前的两倍,这意味着函数完成所需的时间大约是两倍。
  • 谢谢,有道理。我仍然不明白我应该如何弄清楚这一切。这只是我必须测试的书中的三种算法之一。我希望我可以通过输入到 isUnique() 函数中的一组代码来帮助我,我可以在我必须测试的其他两个函数上使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 2014-02-27
  • 1970-01-01
  • 2021-12-18
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多