【发布时间】: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 秒计数吗?
-
我问的问题正是我的书问它的方式。再说一遍。老实说,我不知道该怎么做,我从头文件中谈论,向量需要如何设置,如何计时……没有。我完全不明白。