Image Component Library (ICL)
StackTimer.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/StackTimer.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/Macros.h>
34 #include <ICLUtils/Timer.h>
35 #include <string>
36 #include <cstdio>
37 
38 namespace icl{
39  namespace utils{
41 
103  class StackTimer{
104  public:
107  public:
108  StackTimerNotifier(const char* functionname,
109  bool writeCounts=true,
110  bool writeAvg=true,
111  bool writeMin=true,
112  bool writeMax=true){
113  m_bWriteCounts = writeCounts;
114  m_bWriteAvg = writeAvg;
115  m_bWriteMin = writeMin;
116  m_bWriteMax = writeMax;
117  m_liCount = 0;
118  m_liTime = 0;
119  m_sFunctionName = functionname;
120  m_liMaxTime = 0;
121  m_liMinTime = 2<<29;
122  }
123  std::string getTimeStr(long int l){
124  static char acBuf[100];
125  if(l>1000000){
126  sprintf(acBuf,"%3ld.%1ld s",l/1000000,l/100000-10*(l/1000000));
127  }else if(l>1000){
128  sprintf(acBuf,"%3ld.%1ld ms",l/1000,l/100-10*(l/1000));
129  }else{
130  sprintf(acBuf," %3ld us",l);
131  }
132  return std::string(acBuf);
133  }
136  if(m_bWriteCounts){
137  printf("calls[%8ld] ",m_liCount);
138  }
139  printf("time[%s] ",getTimeStr(m_liTime).c_str());
140  if(m_bWriteAvg){
141  printf("avg[%s] ",getTimeStr(m_liTime/m_liCount).c_str());
142  }
143  if(m_bWriteMin){
144  printf("min[%s] ",getTimeStr(m_liMinTime).c_str());
145  }
146  if(m_bWriteMax){
147  printf("max[%s] ",getTimeStr(m_liMaxTime).c_str());
148  }
149  printf("{%s}\n",m_sFunctionName.c_str());
150  }
152  void incCount(){
153  m_liCount++;
154  }
156  void incTime(long int dt){
157  m_liTime+=dt;
160  }
161  private:
162  long int m_liCount;
163  long int m_liTime;
164  long int m_liMaxTime;
165  long int m_liMinTime;
166  std::string m_sFunctionName;
167 
172  };
175  m_poTimer = new Timer(1);//1=ns
176  m_poNotifier = notifier;
177  notifier->incCount();
178  m_poTimer->start();
179  }
183  delete m_poTimer;
184  }
185  private:
188 
189  };
190 
191  #define BENCHMARK_THIS_SECTION(SECTION_NAME) \
192  static icl::utils::StackTimer::StackTimerNotifier __notifier(#SECTION_NAME); \
193  icl::utils::StackTimer __stacktimer(&__notifier);
194 
195  #define BENCHMARK_THIS_FUNCTION \
196  static icl::utils::StackTimer::StackTimerNotifier __notifier(__FUNCTION__); \
197  icl::utils::StackTimer __stacktimer(&__notifier);
198 
199  #define BENCHMARK_THIS_FUNCTION_LITE \
200  static icl::utils::StackTimer::StackTimerNotifier __notifier(__FUNCTION__,0,0,0,0); \
201  icl::utils::StackTimer __stacktimer(&__notifier);
202  } // namespace utils
203 }
long int m_liTime
Definition: StackTimer.h:163
void start()
alias for startTimer
Definition: Timer.h:70
undocument this line if you encounter any issues!
Definition: Any.h:37
void incTime(long int dt)
adds execution time, USE BENCHMARK_THIS_FUNCTION-MACRO instead
Definition: StackTimer.h:156
StackTimer(StackTimerNotifier *notifier)
StackTimer constructor, USE BENCHMARK_THIS_FUNCTION-MACRO instead.
Definition: StackTimer.h:174
bool m_bWriteAvg
Definition: StackTimer.h:169
#define iclMin(A, B)
Definition: Macros.h:204
Timer * m_poTimer
Definition: StackTimer.h:186
Tool for benchmarking method calls.
Definition: StackTimer.h:103
long int m_liMinTime
Definition: StackTimer.h:165
void incCount()
increments the execution counter, USE BENCHMARK_THIS_FUNCTION-MACRO instead
Definition: StackTimer.h:152
long int m_liCount
Definition: StackTimer.h:162
~StackTimer()
StackTimerNotifier destructor, USE BENCHMARK_THIS_FUNCTION-MACRO instead.
Definition: StackTimer.h:181
std::string m_sFunctionName
Definition: StackTimer.h:166
long int m_liMaxTime
Definition: StackTimer.h:164
Definition: Timer.h:41
bool m_bWriteCounts
Definition: StackTimer.h:168
#define iclMax(A, B)
Definition: Macros.h:207
StackTimerNotifier constructor, USE BENCHMARK_THIS_FUNCTION-MACRO instead.
Definition: StackTimer.h:106
~StackTimerNotifier()
StackTimerNotifier destructor, USE BENCHMARK_THIS_FUNCTION-MACRO instead.
Definition: StackTimer.h:135
long int stopSilent()
stops the timer and returns the overall working time as long int
StackTimerNotifier * m_poNotifier
Definition: StackTimer.h:187
bool m_bWriteMin
Definition: StackTimer.h:170
bool m_bWriteMax
Definition: StackTimer.h:171
std::string getTimeStr(long int l)
Definition: StackTimer.h:123
StackTimerNotifier(const char *functionname, bool writeCounts=true, bool writeAvg=true, bool writeMin=true, bool writeMax=true)
Definition: StackTimer.h:108