创建可变参数模板函数来测量和执行其他函数 [英] Create variadic template function to measure and execute other function

查看:174
本文介绍了创建可变参数模板函数来测量和执行其他函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试实现一个函数,该函数将输入任何其他函数和该函数的有效输入值集合,并返回函数的结果以及打印执行该函数所花费的时间。 p>

这是我到现在为止:

  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<<持续时间< milliseconds<< endl;
return res;
}

我试着运行它:

  int res = measureAndExecute(function< int(vector< int>& vector,< bool&长期),斐波纳契,术语,计算,n-1); 

这是在Fibonacci系列中查找术语的函数。



当我尝试运行它,我得到以下错误:

 错误:expected'函数式转换或类型构造

有人可以给我一个前进的方法或如何进行的想法?

解决方案

我同意@ 101010这是一种不寻常的软件基准化方法。



也就是说,这里是一个解决方案,也适用于函数 void 返回类型(问题中的示例不会与他们一起使用) :

 #include< type_traits> 
#include< iostream>

struct Check {
检查():time1 {std :: chrono :: high_resolution_clock :: now()} {}

〜Check(){
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<<持续时间< milliseconds<< std :: endl;
}

std :: chrono :: high_resolution_clock :: time_point time1;
};


template< typename F,typename ... Tail>
typename std :: result_of< F(Tail& ...)> :: type measureAndExecute(F f,Tail& ... tail){
Check check;
(void)check;
return f(std :: forward< Tail>(tail)...);
}

int f(int i){return i; }
void g(){}

int main(){
measureAndExecute(f,42);
measureAndExecute(g);
}

基本思想是创建一个







$ b

如注释中所述, measureAndExecute 的细化将是:

  template< typename F,typename ... Tail> 
typename std :: result_of< F&&(Tail& ...)> :: type measureAndExecute(F& f,Tail& ... tail){
检查检查;
(void)check;
return std :: forward< F>(f)(std :: forward< Tail>(tail)...)
}


I am currently trying to implement a function that will take as input any other function and a valid set of input values for that function and return the result of the function as well as printing how long it took to execute it.

Here is what I have until now:

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;
}

And I try to run it with something like this:

int res = measureAndExecute(function<int(vector<int>&, vector<bool>&, unsigned long)> fibonacci, terms, calculated, n-1);

Which is a function to find a term in the Fibonacci series.

When I try to run it I get the following error:

error: expected '(' for function-style cast or type construction

Can somebody please give me a way forward or ideas on how to proceed?

解决方案

I agree with @101010 that this is a unusual way of benchmarking a software.

That said, here is a solution that works also with functions havingvoid return type (the example in the question wouldn't have worked with them):

#include<type_traits>
#include<iostream>

struct Check {
    Check(): time1{std::chrono::high_resolution_clock::now()} {}

    ~Check() {
        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;
    }

    std::chrono::high_resolution_clock::time_point time1;
};


template<typename F, typename... Tail>
typename std::result_of<F(Tail&&...)>::type measureAndExecute(F f, Tail&&... tail) {  
    Check check;
    (void)check;
    return f(std::forward<Tail>(tail)...);
}

int f(int i) { return i; }
void g() { }

int main() {
    measureAndExecute(f, 42);
    measureAndExecute(g);
}

The basic idea is to create an instance of Check and exploit its lifetime to measure the time.

EDIT

As mentioned in the comments, a refinement of measureAndExecute would be:

template<typename F, typename... Tail>
typename std::result_of<F&&(Tail&&...)>::type measureAndExecute(F &&f, Tail&&... tail) {  
    Check check;
    (void)check;
    return std::forward<F>(f)(std::forward<Tail>(tail)...);
}

这篇关于创建可变参数模板函数来测量和执行其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆