我正在测试一些算法并对它们进行计时。我想知道如果它运行超过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;
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, n);
if (isUnique(myVect, 0, n) == 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(myVect,0,m),如果它持续时间超过60秒....任何建议?
答案 0 :(得分:0)
当您遇到特殊情况(如超时)时,您可以抛出异常。在您的情况下,您有什么样的例外情况。当您想要退出多级深度调用树而不必单独检查退出返回代码的每个函数时,异常可以正常工作。在下面的例子中,我抛出一个字符串作为例外:
为了防止经常进行时间计算,这里试图限制它们的完成频率。这绝不是科学的,但在我的盒子上它给出了合理的结果。如果你有更好的方式让我知道。
#include <iostream>
#include <vector>
#include <ctime>
#include <chrono>
#include <cstdlib>
#include <random>
#include <algorithm>
bool isUnique(const std::vector<int>& arr, int start, int end, clock_t clock_start,
unsigned long & iterations, unsigned long timecheck) {
if (++iterations > timecheck) {
iterations = 0;
clock_t clock_current = clock();
double time = (double)(clock_current - clock_start) * 1000.0 / CLOCKS_PER_SEC;
if (time > 60000) throw "This function takes too long!";
else std::cout << time << " ms elapsed..." << std::endl;
}
if (start >= end) return true;
if (!isUnique(arr, start, end - 1, clock_start, iterations, timecheck))
return false;
if (!isUnique(arr, start + 1, end, clock_start, iterations, timecheck))
return false;
return (arr[start] != arr[end]);
}
bool isUniqueLoop(const std::vector<int>& arr, int start, int end, clock_t clock_start,
unsigned long & iterations, unsigned long timecheck) {
if (start >= end) return true;
for (int i = start; i < end; i++) {
for (int j = i + 1; j <= end; j++) {
if (++iterations > timecheck) {
iterations = 0;
clock_t clock_current = clock();
double time = (double)(clock_current - clock_start) * 1000.0 / CLOCKS_PER_SEC;
if (time > 60000) throw "This function takes too long!";
else std::cout << time << " ms elapsed..." << std::endl;
}
if (arr[i] == arr[j])return false;
}
}
return true;
}
bool isUniqueSort(const std::vector<int>& arr, int start, int end, clock_t clock_start,
unsigned long & iterations, unsigned long timecheck) {
if (start <= end) return true;
std::vector<int> buf(arr);
sort(buf.begin() + start, buf.begin() + end);
for (int i = start; i < end; i++) {
if (++iterations > timecheck) {
iterations = 0;
clock_t clock_current = clock();
double time = (double)(clock_current - clock_start) * 1000.0 / CLOCKS_PER_SEC;
if (time > 60000) throw "This function takes too long!";
else std::cout << time << " ms elapsed..." << std::endl;
}
if (buf[i] == buf[i + 1]) return false;
}
return true;
}
unsigned long calculate_timecheck() {
// since we want to limit the functions to 60 seconds we need to check how
// long they have been running but we don't want to do that all the time
// so we should only check so often - here is an attemopt to figure out how often
unsigned long upperlimit = 1;
volatile unsigned long i;
clock_t start, current;
double time;
do {
if (upperlimit > ULONG_MAX/2) return ULONG_MAX-1;
upperlimit *= 2;
start = clock();
for (i = 0; i < upperlimit*100000; i++);
current = clock();
time = (double)(current - start) * 1000.0 / CLOCKS_PER_SEC;
std::cout << upperlimit*100000 << " iterations took " << time << " ms." << std::endl;
} while (time<500);
return upperlimit*100000;
}
int main() {
int max = 0;
std::cout << "Enter a number for the Max range: ";
std::cin >> max;
std::default_random_engine randGen(time(0));
std::uniform_int_distribution<int> randNum(0, max);
int n = randNum(randGen);
int m = n;
std::vector<int> myVect;
for (int i = 0; i <= m; i++) {
myVect.push_back(randNum(randGen));
//std::cout << myVect[i] << std::endl;
}
std::cout << "Calculating timeout... " << std::endl;
std::cout << std::endl;
unsigned long timecheck = calculate_timecheck();
std::cout << "Will check for timeout every " << timecheck << " iterations..." << std::endl;
std::cout << std::endl;
std::cout << "------------------------------------" << std::endl;
// recursive algorithm
try {
std::cout << "Recursive Algorithm Test... " << std::endl;
std::cout << std::endl;
clock_t start = clock();
unsigned long iterations = 0;
// isUnique(myVect, 0, n, start, iterations, timecheck);
if (isUnique(myVect, 0, n, start, iterations, timecheck) == true) {
std::cout << "The Vector is Unique! " << std::endl;
}
else {
std::cout << "The Vector is not Unique! " << std::endl;
}
clock_t end = clock();
double time = (double)(end - start) * 1000.0 / CLOCKS_PER_SEC;
std::cout << "CPU Time used for this algorithm: " << time << " ms." << std::endl;
} catch (const char *err) {
std::cout << err << std::endl;
}
std::cout << "------------------------------------" << std::endl;
// iterative algorithm
try {
std::cout << "Iterative Algorithm Test... " << std::endl;
std::cout << std::endl;
clock_t start = clock();
unsigned long iterations = 0;
// isUniqueLoop(myVect, 0, n, start, iterations, timecheck);
if (isUniqueLoop(myVect, 0, n, start, iterations, timecheck) == true) {
std::cout << "The Vector is Unique! " << std::endl;
}
else {
std::cout << "The Vector is not Unique! " << std::endl;
}
clock_t end = clock();
double time = (double)(end - start) * 1000.0 / CLOCKS_PER_SEC;
std::cout << "CPU time used for this algorithm: " << time << " ms." << std::endl;
} catch (const char *err) {
std::cout << err << std::endl;
}
std::cout << "------------------------------------" << std::endl;
// sort algorithm
try {
std::cout << "Sort Algorithm Test... " << std::endl;
std::cout << std::endl;
clock_t start = clock();
unsigned long iterations = 0;
// isUniqueSort(myVect, 0, n, start, iterations, timecheck);
if (isUniqueSort(myVect, 0, n, start, iterations, timecheck) == true) {
std::cout << "The Vector is Unique! " << std::endl;
}
else {
std::cout << "The Vector is not Unique " << std::endl;
}
clock_t end = clock();
double time = (double)(end - start) * 1000.0 / CLOCKS_PER_SEC;
std::cout << "CPU time used for this algorithm: " << time << " ms." << std::endl;
} catch (const char *err) {
std::cout << err << std::endl;
}
std::cout << std::endl;
system("pause");
return 0;
}
#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, clock_t clock_start) {
clock_t clock_current = clock();
double time = (double)(clock_current - clock_start) / CLOCKS_PER_SEC * 1000.0;
if (time > 60000) throw "This function takes too long!";
if (start >= end) return true;
if (!isUnique(arr, start, end - 1, clock_start))
return false;
if (!isUnique(arr, start + 1, end, clock_start))
return false;
return (arr[start] != arr[end]);
}
bool isUniqueLoop(const vector<int>& arr, int start, int end, clock_t clock_start) {
if (start >= end) return true;
for (int i = start; i < end; i++) {
for (int j = i + 1; j <= end; j++) {
clock_t clock_current = clock();
double time = (double)(clock_current - clock_start) / CLOCKS_PER_SEC * 1000.0;
if (time > 60000) throw "This function takes too long!";
if (arr[i] == arr[j])return false;
}
}
return true;
}
bool isUniqueSort(const vector<int>& arr, int start, int end, clock_t clock_start) {
if (start <= end) return true;
vector<int> buf(arr);
sort(buf.begin() + start, buf.begin() + end);
for (int i = start; i < end; i++) {
clock_t clock_current = clock();
double time = (double)(clock_current - clock_start) / CLOCKS_PER_SEC * 1000.0;
if (time > 60000) throw "This function takes too long!";
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;
vector<int> myVect;
for (i = 0; i <= m; i++) {
myVect.push_back(randNum(randGen));
//cout << myVect[i] << endl;
}
try {
cout << "Recursive Algorithm Test... " << endl;
cout << endl;
// recursive algorithm
clock_t start = clock();
isUnique(myVect, 0, n, start);
if (isUnique(myVect, 0, n, start) == 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;
} catch (const char *err) {
cout << err << endl;
}
cout << "------------------------------------" << endl;
try {
cout << "Iterative Algorithm Test... " << endl;
cout << endl;
// iterative algorithm
clock_t start2 = clock();
isUniqueLoop(myVect, 0, n, start2);
if (isUniqueLoop(myVect, 0, n, start2) == 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;
} catch (const char *err) {
cout << err << endl;
}
cout << "------------------------------------" << endl;
try {
cout << "Sort Algorithm Test... " << endl;
cout << endl;
// sort algorithm
clock_t start3 = clock();
isUniqueSort(myVect, 0, n, start3);
if (isUniqueSort(myVect, 0, n, start3) == 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;
} catch (const char *err) {
cout << err << endl;
}
cout << endl;
system("pause");
return 0;
}