任何运算符、函数调用和构造函数的通用持续时间计

Generic duration meter for any operator, function call and constructor

我正在使用模板化的 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";
}

尝试这个让我开始思考。是否有 general/consistent 重写它的方法,以应用于任何类型的计算(不仅仅是构造函数,甚至可能是其他运算符,如 (obj1 < 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() << " ms\n";
}

无论您如何封装实际的函数调用,如果您使 meter 函数 return 由它测量的函数 return 值,则可能是最好的使链接呼叫成为可能 - 但仍然有可能检查每个单独的呼叫之后花费了多长时间。这使得 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