是否有可能编写可被用于测量操作者的其它功能的时间,以及是这样的函数:
// pass an operator as an argument
measureTime(MyClass::operator*, lhs, rhs);
// Pass a function as an argument
measureTime(MyFunction(arg1, arg2));
// or
measureTime(MyFunction, arg1, arg2);
// or other signatures ...
我碰到一个函数模板来做到这一点:
template<typename F, typename... Args>
double measureTime(F func, Args&&... args){
auto t1 = high_resolution_clock::now();
func(std::forward<Args>(args)...);
return high_resolution_clock::now() - t1;
}
不知道如何使用这个(或写一个新的函数)来衡量超载运营商的时间。
问题
- 什么是做到这一点(如果可能的话)的最好方法?
- 如果不可能,还有其他选择吗?
例
我写的大整数运算库,我使用不同的算法和想测量(和比较)的时间(看看他们的行为)通过不同长度的整数时, ...
这是我的课的一部分:
class bigint {
public:
int compare(const bigint& other) const;
bigint operator*(const bigint& rhs) const;
bigint& operator++();
bigint otherAlgorithms(const bigint& other) const;
// and more ...
}
我也有很多的输入数据,并想通过那些在for
循环内的数据并打印出时间。
这就是为什么我在寻找一个通用的函数,可以打印任何函数/操作符的时间。
如果使用'的std :: invoke'而不是自己这个调用该函数应与成员的工作职能 - – tkausl
除非'MyFunc'返回一个函数,传递'MyFunction的(ARG1,ARG2)'将结果传递表达式 – Vivick
运算符仍然是一个函数,所以传递它应该没有问题。像'运营商<<(a,b)' – AndyG