Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/StackTimer.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 00034 #include <ICLUtils/Timer.h> 00035 #include <string> 00036 #include <ICLUtils/Macros.h> 00037 #include <cstdio> 00038 00039 namespace icl{ 00040 namespace utils{ 00042 00101 class StackTimer{ 00102 public: 00104 class StackTimerNotifier{ 00105 public: 00106 StackTimerNotifier(const char* functionname, 00107 bool writeCounts=true, 00108 bool writeAvg=true, 00109 bool writeMin=true, 00110 bool writeMax=true){ 00111 m_bWriteCounts = writeCounts; 00112 m_bWriteAvg = writeAvg; 00113 m_bWriteMin = writeMin; 00114 m_bWriteMax = writeMax; 00115 m_liCount = 0; 00116 m_liTime = 0; 00117 m_sFunctionName = functionname; 00118 m_liMaxTime = 0; 00119 m_liMinTime = 2<<29; 00120 } 00121 std::string getTimeStr(long int l){ 00122 static char acBuf[100]; 00123 if(l>1000000){ 00124 sprintf(acBuf,"%3ld.%1ld s",l/1000000,l/100000-10*(l/1000000)); 00125 }else if(l>1000){ 00126 sprintf(acBuf,"%3ld.%1ld ms",l/1000,l/100-10*(l/1000)); 00127 }else{ 00128 sprintf(acBuf," %3ld ns",l); 00129 } 00130 return std::string(acBuf); 00131 } 00133 ~StackTimerNotifier(){ 00134 if(m_bWriteCounts){ 00135 printf("calls[%8ld] ",m_liCount); 00136 } 00137 printf("time[%s] ",getTimeStr(m_liTime).c_str()); 00138 if(m_bWriteAvg){ 00139 printf("avg[%s] ",getTimeStr(m_liTime/m_liCount).c_str()); 00140 } 00141 if(m_bWriteMin){ 00142 printf("min[%s] ",getTimeStr(m_liMinTime).c_str()); 00143 } 00144 if(m_bWriteMax){ 00145 printf("max[%s] ",getTimeStr(m_liMaxTime).c_str()); 00146 } 00147 printf("{%s}\n",m_sFunctionName.c_str()); 00148 } 00150 void incCount(){ 00151 m_liCount++; 00152 } 00154 void incTime(long int dt){ 00155 m_liTime+=dt; 00156 m_liMinTime = iclMin(dt,m_liMinTime); 00157 m_liMaxTime = iclMax(dt,m_liMaxTime); 00158 } 00159 private: 00160 long int m_liCount; 00161 long int m_liTime; 00162 long int m_liMaxTime; 00163 long int m_liMinTime; 00164 std::string m_sFunctionName; 00165 00166 bool m_bWriteCounts; 00167 bool m_bWriteAvg; 00168 bool m_bWriteMin; 00169 bool m_bWriteMax; 00170 }; 00172 StackTimer(StackTimerNotifier *notifier){ 00173 m_poTimer = new Timer(1);//1=ns 00174 m_poNotifier = notifier; 00175 notifier->incCount(); 00176 m_poTimer->start(); 00177 } 00179 ~StackTimer(){ 00180 m_poNotifier->incTime(m_poTimer->stopSilent()); 00181 delete m_poTimer; 00182 } 00183 private: 00184 Timer *m_poTimer; 00185 StackTimerNotifier* m_poNotifier; 00186 00187 }; 00188 00189 #define BENCHMARK_THIS_SECTION(SECTION_NAME) \ 00190 static StackTimer::StackTimerNotifier __notifier(#SECTION_NAME); \ 00191 StackTimer __stacktimer(&__notifier); 00192 00193 #define BENCHMARK_THIS_FUNCTION \ 00194 static StackTimer::StackTimerNotifier __notifier(__FUNCTION__); \ 00195 StackTimer __stacktimer(&__notifier); 00196 00197 #define BENCHMARK_THIS_FUNCTION_LITE \ 00198 static StackTimer::StackTimerNotifier __notifier(__FUNCTION__,0,0,0,0); \ 00199 StackTimer __stacktimer(&__notifier); 00200 } // namespace utils 00201 }