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