lundi 1 août 2016

Create variadic template function to measure and execute other function

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?

Aucun commentaire:

Enregistrer un commentaire