我正在使用模板化的 meter 函数(请参见下文)来测量函数的经过时间。 然后,我也想将其用于构造函数。
据我所知,没有办法直接将类型作为函数参数传递。因此,我想出了一种解决方法,仅将其作为模板参数传递(最小示例):
template <typename T, typename ... P>
auto meter(T t, P ... p) {
auto t1 = high_resolution_clock::now();
t(p...);
auto t2 = high_resolution_clock::now();
auto dif = t2-t1;
return duration_cast<microseconds>(dif);
}
template <typename T, typename ... P>
auto meter(P ... p) {
auto t1 = high_resolution_clock::now();
auto t = T(p...);
auto t2 = high_resolution_clock::now();
auto dif = t2-t1;
return duration_cast<microseconds>(dif);
}
int main() {
auto d = meter(g, 1.0, 20.0); //meter the function call g(1.0, 20.0)
std::cout << "Ellapsed time: " << d.count() << " microseconds\n";
d = meter(complex_obj{2}); //meter () operator of complex_obj, assuming complex_obj{int} is trivial;
std::cout << "Ellapsed time: " << d.count() << " microseconds\n";
d = meter<complex_obj>(); //meter constructor complex_obj();
std::cout << "Ellapsed time: " << d.count() << " microseconds\n";
}
尝试这个让我开始思考。是否有一种通用/一致的方式重写它,以适用于任何类型的计算(不仅是构造函数,甚至还可能是其他运算符,例如(obj1 很抱歉,如果这个问题变得广泛,我的主要问题是,对于函数和构造函数,是否有一种方法可以统一 meter 调用的语法。
答案 0 :(得分:2)
您可以将要测量的代码包装在lambda中(自C ++ 11起):
#include <chrono>
#include <iostream>
template<class F>
auto meter(F&& f) {
auto t1 = std::chrono::high_resolution_clock::now();
f();// <-- operator() of the lambda
auto t2 = std::chrono::high_resolution_clock::now();
auto dif = t2-t1;
return std::chrono::duration_cast<std::chrono::microseconds>(dif);
}
void g(double x, double y) {
std::cout << "g(" << x << ", " << y << ")\n";
}
int main() {
double x = 1.0;
auto d = meter([&] {
// This comment is inside the *body* of the lambda.
// Code of the {body} is executed upon `operator()`.
g(x, 20.0);// note that you can use `x` here thanks to the capture-default `[&]`
});
std::cout << "time: " << d.count() << " ms\n";
}
答案 1 :(得分:1)
无论如何封装实际的函数调用,最好是使meter
函数返回要测量的函数返回的值以使其可以链接调用,但这仍然是最好的选择。检查每个单独通话之后花费多长时间的可能性。从理论上讲,这使RVO /复制删除功能得以实现,因此不会降低代码的速度。示例:
#include <chrono>
#include <iostream>
#include <thread> // for debug sleeps only
using namespace std::chrono;
template<typename D, typename F, typename... P>
auto meter(D& dur, F func, P&&... params) {
auto start = high_resolution_clock::now();
auto retval = func(std::forward<P>(params)...);
// put duration in the duration reference
dur = duration_cast<D>(high_resolution_clock::now() - start);
// and return func()'s return value
return retval;
}
namespace m {
double add(double a, double b) {
std::this_thread::sleep_for(milliseconds(10));
return a + b;
}
double sub(double a, double b) {
std::this_thread::sleep_for(milliseconds(11));
return a - b;
}
double mul(double a, double b) {
std::this_thread::sleep_for(milliseconds(12));
return a * b;
}
double div(double a, double b) {
std::this_thread::sleep_for(milliseconds(13));
return a / b;
}
} // namespace m
int main() {
milliseconds Add, Sub, Mul, Div;
// chaining calls for this calculation:
// (1000 / (100 * (4.3 - (1.1+2.2))))
auto result = meter(Div, m::div,
1000.0, meter(Mul, m::mul,
100.0, meter(Sub, m::sub,
4.3, meter(Add, m::add,
1.1, 2.2)
)
)
);
std::cout << "Add: " << Add.count() << " ms.\n";
std::cout << "Sub: " << Sub.count() << " ms.\n";
std::cout << "Mul: " << Mul.count() << " ms.\n";
std::cout << "Div: " << Div.count() << " ms.\n";
std::cout << result << "\n";
}
可能的输出:
Add: 10 ms.
Sub: 11 ms.
Mul: 12 ms.
Div: 13 ms.
10