任何操作员,功能调用和构造函数的通用持续时间表



i使用了模板化函数(请参见下文(来测量函数的经过的时间。然后,我还想将其用于构造函数。

据我所知,无法将类型直接传递为函数参数。因此,我想出了这个解决方法,仅作为模板参数(最小示例(将其传递给:

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() << " microsecondsn";
    d = meter(complex_obj{2}); //meter () operator of complex_obj, assuming complex_obj{int} is trivial;
    std::cout << "Ellapsed time: " << d.count() << " microsecondsn";
    d = meter<complex_obj>(); //meter constructor complex_obj();
    std::cout << "Ellapsed time: " << d.count() << " microsecondsn";
}

尝试这个让我思考。是否有一种适用于任何类型的计算的一般/一致的方法(不仅是构造函数,而且甚至是其他运营商,例如(obj1&lt; obj2(?我注意到,我已经(偶然地(支持了((结构的操作员。

对不起,如果这个问题变得广泛,我的主要问题是,是否有一种方法来团结 meter 呼叫的语法,以供函数和构造函数。

您可以包装要在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() << " msn";
}

,但是您封装了实际函数调用,如果您使meter函数返回由函数返回的函数以使链呼叫的函数返回的值 - 但是,仍然有可能检查每个单个电话之后需要多长时间。从理论上讲,RVO/Copy Elision可以启动它,因此不能尽可能减慢代码。示例:

#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

最新更新