[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

timing.hxx
1 /************************************************************************/
2 /* */
3 /* Copyright 2008-2009 by Ullrich Koethe */
4 /* Cognitive Systems Group, University of Hamburg, Germany */
5 /* */
6 /* This file is part of the VIGRA computer vision library. */
7 /* The VIGRA Website is */
8 /* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */
9 /* Please direct questions, bug reports, and contributions to */
10 /* ullrich.koethe@iwr.uni-heidelberg.de or */
11 /* vigra@informatik.uni-hamburg.de */
12 /* */
13 /* Permission is hereby granted, free of charge, to any person */
14 /* obtaining a copy of this software and associated documentation */
15 /* files (the "Software"), to deal in the Software without */
16 /* restriction, including without limitation the rights to use, */
17 /* copy, modify, merge, publish, distribute, sublicense, and/or */
18 /* sell copies of the Software, and to permit persons to whom the */
19 /* Software is furnished to do so, subject to the following */
20 /* conditions: */
21 /* */
22 /* The above copyright notice and this permission notice shall be */
23 /* included in all copies or substantial portions of the */
24 /* Software. */
25 /* */
26 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
27 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
28 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
29 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
30 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
31 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
32 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
33 /* OTHER DEALINGS IN THE SOFTWARE. */
34 /* */
35 /************************************************************************/
36 
37 
38 #ifndef VIGRA_TIMING_HXX
39 #define VIGRA_TIMING_HXX
40 #ifndef VIGRA_NO_TIMING
41 
42 #include <iostream>
43 #include <sstream>
44 #ifdef MULTI_TICTOC
45  #include <vector>
46 #endif
47 // usage:
48 // void time_it()
49 // {
50 // USETICTOC;
51 // TIC;
52 // ...
53 // std::cerr << TOC << " for time_it\n";
54 // }
55 
56 #ifdef WIN32
57 
58  #include "windows.h"
59 
60  namespace {
61 
62  inline double queryTimerUnit()
63  {
64  LARGE_INTEGER frequency;
65  QueryPerformanceFrequency(&frequency);
66  return 1000.0 / frequency.QuadPart;
67  }
68 
69  inline double tic_toc_diff_num(LARGE_INTEGER const & tic)
70  {
71  LARGE_INTEGER toc;
72  QueryPerformanceCounter(&toc);
73  static double unit = queryTimerUnit();
74  return ((toc.QuadPart - tic.QuadPart) * unit);
75  }
76 
77  inline std::string tic_toc_diff_string(LARGE_INTEGER const & tic)
78  {
79  double diff = tic_toc_diff_num(tic);
80  std::stringstream s;
81  s << diff << " msec";
82  return s.str();
83  }
84 
85  inline void tic_toc_diff(LARGE_INTEGER const & tic)
86  {
87  std::cerr << tic_toc_diff_string(tic) <<std::endl;
88  }
89 
90  } // unnamed namespace
91 
92 #ifndef MULTI_TICTOC
93  #define USETICTOC LARGE_INTEGER tic_timer;
94  #define TIC QueryPerformanceCounter(&tic_timer);
95  #define TOC tic_toc_diff (tic_timer);
96  #define TOCN tic_toc_diff_num (tic_timer);
97  #define TOCS tic_toc_diff_string(tic_timer);
98 #else
99  #define USETICTOC std::vector<LARGE_INTEGER> tic_timer;
100  #define TIC tic_timer.push_back(LARGE_INTEGER());\
101  QueryPerformanceCounter(&(tic_timer.back()));
102  #define TOC tic_toc_diff (tic_timer.back());\
103  tic_timer.pop_back();
104  #define TOCN tic_toc_diff_num (tic_timer.back());\
105  tic_timer.pop_back();
106  #define TOCS tic_toc_diff_string(tic_timer.back());\
107  tic_timer.pop_back();
108 #endif
109 
110 #else
111 
112  #if defined(VIGRA_HIRES_TIMING) && !defined(__CYGWIN__)
113  // requires linking against librt
114 
115  #include <time.h>
116 
117  namespace {
118 
119  inline double tic_toc_diff_num(timespec const & tic)
120  {
121  timespec toc;
122  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &toc);
123  return ((toc.tv_sec*1000.0 + toc.tv_nsec/1000000.0) -
124  (tic.tv_sec*1000.0 + tic.tv_nsec/1000000.0));
125  }
126 
127  inline std::string tic_toc_diff_string(timeval const & tic)
128  {
129  double diff = tic_toc_diff_num(tic);
130  std::stringstream s;
131  s << diff << " msec";
132  return s.str();
133  }
134 
135  inline void tic_toc_diff(timeval const & tic)
136  {
137  std::cerr << tic_toc_diff_string(tic) << std::endl;
138  }
139  } // unnamed namespace
140 
141 #ifndef MULTI_TICTOC
142  #define USETICTOC timespec tic_timer;
143  #define TIC clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tic_timer);
144  #define TOC tic_toc_diff (tic_timer);
145  #define TOCN tic_toc_diff_num (tic_timer);
146  #define TOCS tic_toc_diff_string(tic_timer);
147 #else
148 
149  #define USETICTOC std::vector<timespec> tic_timer;
150  #define TIC tic_timer.push_back(timespec());\
151  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(tic_timer.back()));
152  #define TOC tic_toc_diff (tic_timer.back());\
153  tic_timer.pop_back();
154  #define TOCN tic_toc_diff_num (tic_timer.back());\
155  tic_timer.pop_back();
156  #define TOCS tic_toc_diff_string(tic_timer.back());\
157  tic_timer.pop_back();
158 #endif
159  #else
160 
161  #include <sys/time.h>
162 
163  namespace {
164 
165  inline double tic_toc_diff_num(timeval const & tic)
166  {
167  timeval toc;
168  gettimeofday(&toc, NULL);
169  return ((toc.tv_sec*1000.0 + toc.tv_usec/1000.0) -
170  (tic.tv_sec*1000.0 + tic.tv_usec/1000.0));
171  }
172  inline std::string tic_toc_diff_string(timeval const & tic)
173  {
174  double diff = tic_toc_diff_num(tic);
175  std::stringstream s;
176  s << diff << " msec";
177  return s.str();
178  }
179  inline void tic_toc_diff(timeval const & tic)
180  {
181  std::cerr << tic_toc_diff_string(tic)<< std::endl;
182  }
183 
184  } // unnamed namespace
185 
186 #ifndef MULTI_TICTOC
187  #define USETICTOC timeval tic_timer;
188  #define TIC gettimeofday (&tic_timer, NULL);
189  #define TOC tic_toc_diff (tic_timer);
190  #define TOCN tic_toc_diff_num (tic_timer);
191  #define TOCS tic_toc_diff_string(tic_timer);
192 #else
193 
194  #define USETICTOC std::vector<timeval> tic_timer;
195  #define TIC tic_timer.push_back(timeval());\
196  gettimeofday(&(tic_timer.back()), NULL);
197  #define TOC tic_toc_diff (tic_timer.back());\
198  tic_timer.pop_back();
199  #define TOCN tic_toc_diff_num (tic_timer.back());\
200  tic_timer.pop_back();
201  #define TOCS tic_toc_diff_string(tic_timer.back());\
202  tic_timer.pop_back();
203 #endif
204 
205  #endif // VIGRA_HIRES_TIMING
206 
207 #endif // WIN32
208 
209 
210 
211 
212 #else // NDEBUG
213 
214 #define USETICTOC
215 #define TIC
216 #define TOC
217 #define TOCN
218 #define TICS
219 #endif // NDEBUG
220 
221 #endif // VIGRA_TIMING_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.1 (Tue Jul 10 2012)