c253aba875f53d0b19832fef8e9d366ffdde2f85
[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  * \note requires BLI_utildefines.h for 'AT'.
34  */
35
36 #ifndef __PIL_TIME_UTILDEFINES_H__
37 #define __PIL_TIME_UTILDEFINES_H__
38
39 #define TIMEIT_START(var)                                                     \
40         {                                                                         \
41                 double _timeit_##var = PIL_check_seconds_timer();                     \
42                 printf("time start (" #var "):  " AT "\n");                           \
43                 fflush(stdout);                                                       \
44                 { (void)0
45
46 /**
47  * \return the time since TIMEIT_START was called.
48  */
49 #define TIMEIT_VALUE(var) (float)(PIL_check_seconds_timer() - _timeit_##var)
50
51 #define TIMEIT_VALUE_PRINT(var)                                               \
52         {                                                                         \
53                 printf("time update(" #var "): %.6f" "  " AT "\n", TIMEIT_VALUE(var));\
54                 fflush(stdout);                                                       \
55         } (void)0
56
57 #define TIMEIT_END(var)                                                       \
58         }                                                                         \
59         printf("time end   (" #var "): %.6f" "  " AT "\n", TIMEIT_VALUE(var));    \
60         fflush(stdout);                                                           \
61 } (void)0
62
63 /**
64  * Given some function/expression:
65  *   TIMEIT_BENCH(some_function(), some_unique_description);
66  */
67 #define TIMEIT_BENCH(expr, id)                                                \
68         {                                                                         \
69                 TIMEIT_START(id);                                                     \
70                 (expr);                                                               \
71                 TIMEIT_END(id);                                                       \
72         } (void)0
73
74 #define TIMEIT_BLOCK_INIT(id) \
75         double _timeit_var_##id = 0; \
76         (void) 0
77
78 #define TIMEIT_BLOCK_START(id)                                                \
79         {                                                                         \
80                 double _timeit_block_start_##id = PIL_check_seconds_timer();          \
81                 { (void)0
82
83 #define TIMEIT_BLOCK_END(id)                                                  \
84                 }                                                                     \
85                 _timeit_var_##id += (PIL_check_seconds_timer() -                      \
86                                      _timeit_block_start_##id);                       \
87         } (void)0
88
89 #define TIMEIT_BLOCK_STATS(id)                                                \
90         {                                                                         \
91                 printf("%s time (in seconds): %f\n", #id, _timeit_var_##id);          \
92                 fflush(stdout);                                                       \
93         } (void)0
94
95 #endif  /* __PIL_TIME_UTILDEFINES_H__ */