我目前正在尝试实现一个函数,该函数将任何其他函数和该函数的一组有效输入值作为输入,并返回该函数的结果以及打印执行该函数所需的时间。
这是我到目前为止所拥有的:
template<typename T, typename... Tail>
T measureAndExecute(const function<T(Tail...)> f, Tail... tail) {
high_resolution_clock::time_point time1 = high_resolution_clock::now();
T res = f(tail...);
high_resolution_clock::time_point time2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(time2 - time1).count();
cout << duration << " milliseconds" << endl;
return res;
}
我尝试用这样的东西来运行它:
int res = measureAndExecute(function<int(vector<int>&, vector<bool>&, unsigned long)> fibonacci, terms, calculated, n-1);
这是一个在斐波那契数列中查找项的函数。
当我尝试运行它时,出现以下错误:
error: expected '(' for function-style cast or type construction
有人可以给我一个前进的方向或如何继续的想法吗?
这是一种非常幼稚的基准测试方法。我建议你看一下here https://stackoverflow.com/a/21995693/2352671对于更高级的东西。不过,如果你想坚持它,你应该更改为:
template<typename F, typename... Tail>
auto measureAndExecute(F f, Tail&&... tail) -> typename std::result_of<F(Tail&&...)>::type {
std::chrono::high_resolution_clock::time_point time1 = std::chrono::high_resolution_clock::now();
auto res = f(std::forward<Tail>(tail)...);
std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(time2 - time1).count();
std::cout << duration << " milliseconds" << std::endl;
return res;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)