2

考虑以下代码:LWS

#include <iostream>
#include <chrono>
#include <cmath>
#include <ctime>
#include <cstdlib>

template <class Counter, class Function, class... Args>
inline double benchmark(const Counter& counter, Function&& f, Args&&... args)
{
    const std::chrono::high_resolution_clock::time_point marker 
    = std::chrono::high_resolution_clock::now();

    for (Counter i = Counter(); i < counter; ++i) {
        f(args...);
    }

    return std::chrono::duration_cast<std::chrono::duration<double> >
    (std::chrono::high_resolution_clock::now()-marker).count();
}

int main(int argc, char* argv[])
{
   srand(time(nullptr));
   double y = rand()%10+1;
   std::cout<<benchmark(1000000, [](double x){return std::sin(x);}, y)<<"\n";
   return 0;
}

该函数benchmark测量函数的执行时间。问题是函数在优化过程中被删除为空语句。有没有办法强制函数真正执行?

编辑:1)我正在寻找标准 C++ 中的解决方案(没有特定于编译器的指令) 2)如果f可以保持尽可能通用(例如 void 返回类型)会更好

4

2 回答 2

3

我使用 volatile 临时找到了这个解决方案:

#include <iostream>
#include <chrono>
#include <cmath>

template <class Clock = std::chrono::high_resolution_clock, class Counter, class Function, class... Args>
inline double benchmark(const Counter& counter, Function&& f, Args&&... args)
{
    volatile decltype(f(args...)) temporary = decltype(f(args...))();
    const typename Clock::time_point marker = Clock::now();
    for (Counter i = Counter(); i < counter; ++i) {
      temporary = f(args...);
    }
    return std::chrono::duration<double>(Clock::now()-marker).count();
}

int main(int argc, char* argv[])
{
   std::cout<<benchmark(1000000000, [](double x){return std::sin(x);}, 3.)<<"\n";
   return 0;
}

如果您知道如何改进此代码,请发表评论。

于 2013-03-16T05:57:42.953 回答
0

既然(匿名)函数返回一个值,为什么不捕获该值benchmark并用它做一些微不足道的事情,比如将它添加到通过引用传入的值中?像这样:

template <class Counter, class Function, class... Args>
inline double benchmark(double& sum, const Counter& counter, Function&& f, Args&&... args)
{
    const std::chrono::high_resolution_clock::time_point marker 
    = std::chrono::high_resolution_clock::now();

    for (Counter i = Counter(); i < counter; ++i) {
        sum += f(args...);
    }

    return std::chrono::duration_cast<std::chrono::duration<double> >
    (std::chrono::high_resolution_clock::now()-marker).count();
}

我认为编译器现在将很难优化函数调用(假设您打印或以某种方式使用 sum in main())。

于 2013-03-16T03:21:03.697 回答