Vincent
Vincent

Reputation: 60381

Prevent null-statement optimization for benchmark

Consider the following code : 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;
}

The function benchmark measure the execution time of a function. The problem is that the function is erased as a null statement during optimization passes. Is there a way to force the function to be really executed ?

EDIT : 1) I am searching for a solution in a standard C++ (no compiler-specific instructions) 2) It would be better if f could remain as generic as possible (void return type for example)

Upvotes: 2

Views: 274

Answers (2)

Vincent
Vincent

Reputation: 60381

I've found this solution using a volatile temporary :

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

Please comment if you know how to improve this code.

Upvotes: 3

John Zwinck
John Zwinck

Reputation: 249153

Since the (anonymous) function returns a value, why not capture that value in benchmark and do something trivial with it, like add it to a value which was passed in by reference? Like so:

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

I think the compiler will now have a hard time optimizing the function calls away (assuming you print or somehow use the sum in main()).

Upvotes: 0

Related Questions