Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
src/utils/benchmark/benchmark.h File Reference
#include <limits>
#include "color.h"
#include "utils/timer/timer.h"
#include "utils/static_assert/static_assert.h"
Include dependency graph for benchmark.h:

Go to the source code of this file.

Namespaces

namespace  utils
namespace  utils::benchmark

Defines

#define BENCHMARK(times, code)
#define AUTO_BENCHMARK(code)

Functions

void utils::benchmark::printBenchmarkResults (long long int times, double run_time_sec, const char *function_str)

Variables

const double utils::benchmark::MIN_BENCHMARK_TIME = 1.5

Define Documentation

#define AUTO_BENCHMARK (   code)
Value:
{ \
    ::utils::timer::Timer timer = ::utils::timer::Timer(); \
    long long int times = 0; \
    int i = 0; \
    while ((timer.elapsed_time_sec() < ::utils::benchmark::MIN_BENCHMARK_TIME) &&  \
        (i < std::numeric_limits<long long int>::digits)) { \
      long long int t = 1LL << i; \
        for (long long q__ = 0; q__ < t; q__++) { \
            code; \
        } \
      times += t; \
      i++; \
    } \
    ::utils::benchmark::printBenchmarkResults(\
        times, timer.elapsed_time_sec(), #code); \
  }

Macro for benchmarking the code The benchmark runs the code several times depenging on the speed of execution and prints nicely formatted report to stdout. Usage:

   AUTO_BENCHMARK(my_function());
 

calls the function my_function for approximately MIN_BENCHMARK_TIME seconds.

Note: This macro uses exponential decay to guess the correct number of iterations, and so that instrumentation is not big.

Warning:
Sometimes compiler optimizes out parts of you code!
Parameters:
codecode that should be run
#define BENCHMARK (   times,
  code 
)
Value:
{ \
    STATIC_ASSERT(std::numeric_limits<typeof(times)>::is_integer, "") \
    ::utils::timer::Timer timer = ::utils::timer::Timer(); \
    for (typeof(times) q__= 0; q__ < times; q__++) { \
        code;\
    } \
    ::utils::benchmark::printBenchmarkResults(\
        times, timer.elapsed_time_sec(), #code); \
  }

Macro for benchmarking code.

The benchmark runs specified times the code and prints nicely formatted report to stdout. Usage: BENCHMARK(100, my_function()); calls 100 times function my_function.

Note: "function" does not need to be the function, in fact, it may be sequence of statements of event the {} block.

Warning:
Sometimes compiler optimizes out parts of you code!
Parameters:
timesnumber of times the code should be run
codecode that should be run
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines