Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
|
00001 #ifndef H_BENCHMARK 00002 #define H_BENCHMARK 00003 00008 #include <limits> 00009 #include "color.h" 00010 #include "utils/timer/timer.h" 00011 #include "utils/static_assert/static_assert.h" 00012 00013 namespace utils { 00014 namespace benchmark { 00015 00026 const double MIN_BENCHMARK_TIME = 1.5; 00027 00034 void printBenchmarkResults( 00035 long long int times, 00036 double run_time_sec, 00037 const char* function_str) 00038 { 00039 bool isLongEnough = run_time_sec > MIN_BENCHMARK_TIME; 00040 00041 color::Color color = isLongEnough ? color::CYAN : color::PINK; 00042 double avg_time_sec = run_time_sec / times; 00043 double times_per_sec = 1 / avg_time_sec; 00044 00045 color::colorPrintf(color, 00046 "BENCHMARK: time %6.2lfs avg.time %10.5lfms " 00047 "%12.5lf times/ms, %10lldx %s\n", 00048 run_time_sec, avg_time_sec*1000, times_per_sec/1000, times, function_str); 00049 } 00050 00051 00072 #define BENCHMARK(times, code) { \ 00073 STATIC_ASSERT(std::numeric_limits<typeof(times)>::is_integer, "") \ 00074 ::utils::timer::Timer timer = ::utils::timer::Timer(); \ 00075 for (typeof(times) q__= 0; q__ < times; q__++) { \ 00076 code;\ 00077 } \ 00078 ::utils::benchmark::printBenchmarkResults(\ 00079 times, timer.elapsed_time_sec(), #code); \ 00080 } 00081 00101 #define AUTO_BENCHMARK(code) { \ 00102 ::utils::timer::Timer timer = ::utils::timer::Timer(); \ 00103 long long int times = 0; \ 00104 int i = 0; \ 00105 while ((timer.elapsed_time_sec() < ::utils::benchmark::MIN_BENCHMARK_TIME) && \ 00106 (i < std::numeric_limits<long long int>::digits)) { \ 00107 long long int t = 1LL << i; \ 00108 for (long long q__ = 0; q__ < t; q__++) { \ 00109 code; \ 00110 } \ 00111 times += t; \ 00112 i++; \ 00113 } \ 00114 ::utils::benchmark::printBenchmarkResults(\ 00115 times, timer.elapsed_time_sec(), #code); \ 00116 } 00117 00118 } // namespace benchmark 00119 } // namespace utils 00120 #endif