1 /* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License 4 * as published by the Free Software Foundation; either version 2 5 * of the License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software Foundation, 14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 15 * 16 * The Original Code is Copyright (C) 2013 Blender Foundation. 17 * All rights reserved. 18 */ 19 20 /** \file 21 * \ingroup bli 22 * \brief Utility defines for timing/benchmarks. 23 */ 24 25 #pragma once 26 27 #include "BLI_utildefines.h" /* for AT */ 28 #include "PIL_time.h" /* for PIL_check_seconds_timer */ 29 30 #define TIMEIT_START(var) \ 31 { \ 32 double _timeit_##var = PIL_check_seconds_timer(); \ 33 printf("time start (" #var "): " AT "\n"); \ 34 fflush(stdout); \ 35 { \ 36 (void)0 37 38 /** 39 * \return the time since TIMEIT_START was called. 40 */ 41 #define TIMEIT_VALUE(var) (float)(PIL_check_seconds_timer() - _timeit_##var) 42 43 #define TIMEIT_VALUE_PRINT(var) \ 44 { \ 45 printf("time update (" #var \ 46 "): %.6f" \ 47 " " AT "\n", \ 48 TIMEIT_VALUE(var)); \ 49 fflush(stdout); \ 50 } \ 51 (void)0 52 53 #define TIMEIT_END(var) \ 54 } \ 55 printf("time end (" #var \ 56 "): %.6f" \ 57 " " AT "\n", \ 58 TIMEIT_VALUE(var)); \ 59 fflush(stdout); \ 60 } \ 61 (void)0 62 63 /** 64 * _AVERAGED variants do same thing as their basic counterpart, 65 * but additionally add elapsed time to an averaged static value, 66 * useful to get sensible timing of code running fast and often. 67 */ 68 #define TIMEIT_START_AVERAGED(var) \ 69 { \ 70 static float _sum_##var = 0.0f; \ 71 static float _num_##var = 0.0f; \ 72 double _timeit_##var = PIL_check_seconds_timer(); \ 73 printf("time start (" #var "): " AT "\n"); \ 74 fflush(stdout); \ 75 { \ 76 (void)0 77 78 #define TIMEIT_AVERAGED_VALUE(var) (_num##var ? (_sum_##var / _num_##var) : 0.0f) 79 80 #define TIMEIT_END_AVERAGED(var) \ 81 } \ 82 const float _delta_##var = TIMEIT_VALUE(var); \ 83 _sum_##var += _delta_##var; \ 84 _num_##var++; \ 85 printf("time end (" #var \ 86 "): %.6f" \ 87 " " AT "\n", \ 88 _delta_##var); \ 89 printf("time averaged (" #var "): %.6f (total: %.6f, in %d runs)\n", \ 90 (_sum_##var / _num_##var), \ 91 _sum_##var, \ 92 (int)_num_##var); \ 93 fflush(stdout); \ 94 } \ 95 (void)0 96 97 /** 98 * Given some function/expression: 99 * TIMEIT_BENCH(some_function(), some_unique_description); 100 */ 101 #define TIMEIT_BENCH(expr, id) \ 102 { \ 103 TIMEIT_START(id); \ 104 (expr); \ 105 TIMEIT_END(id); \ 106 } \ 107 (void)0 108 109 #define TIMEIT_BLOCK_INIT(id) double _timeit_var_##id = 0 110 111 #define TIMEIT_BLOCK_START(id) \ 112 { \ 113 double _timeit_block_start_##id = PIL_check_seconds_timer(); \ 114 { \ 115 (void)0 116 117 #define TIMEIT_BLOCK_END(id) \ 118 } \ 119 _timeit_var_##id += (PIL_check_seconds_timer() - _timeit_block_start_##id); \ 120 } \ 121 (void)0 122 123 #define TIMEIT_BLOCK_STATS(id) \ 124 { \ 125 printf("%s time (in seconds): %f\n", #id, _timeit_var_##id); \ 126 fflush(stdout); \ 127 } \ 128 (void)0 129