Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
StackTimer.h
Go to the documentation of this file.
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.LGPL **
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 #include <ICLUtils/Macros.h>
00034 #include <ICLUtils/Timer.h>
00035 #include <string>
00036 #include <cstdio>
00037 
00038 namespace icl{
00039   namespace utils{
00041 
00100     class StackTimer{
00101     public:
00103       class StackTimerNotifier{
00104         public:
00105         StackTimerNotifier(const char* functionname, 
00106                            bool writeCounts=true, 
00107                            bool writeAvg=true,
00108                            bool writeMin=true,
00109                            bool writeMax=true){
00110           m_bWriteCounts = writeCounts;
00111           m_bWriteAvg = writeAvg;
00112           m_bWriteMin = writeMin;
00113           m_bWriteMax = writeMax;
00114           m_liCount = 0;
00115           m_liTime = 0;
00116           m_sFunctionName = functionname;
00117           m_liMaxTime = 0;
00118           m_liMinTime = 2<<29;
00119         }
00120         std::string getTimeStr(long int l){
00121           static char acBuf[100];
00122           if(l>1000000){
00123             sprintf(acBuf,"%3ld.%1ld  s",l/1000000,l/100000-10*(l/1000000));
00124           }else if(l>1000){
00125             sprintf(acBuf,"%3ld.%1ld ms",l/1000,l/100-10*(l/1000));
00126           }else{
00127             sprintf(acBuf,"  %3ld ns",l);
00128           }
00129           return std::string(acBuf);
00130         }
00132         ~StackTimerNotifier(){
00133           if(m_bWriteCounts){
00134             printf("calls[%8ld]  ",m_liCount);
00135           }
00136           printf("time[%s]  ",getTimeStr(m_liTime).c_str());
00137           if(m_bWriteAvg){
00138             printf("avg[%s]  ",getTimeStr(m_liTime/m_liCount).c_str());
00139           }
00140           if(m_bWriteMin){
00141             printf("min[%s]  ",getTimeStr(m_liMinTime).c_str());
00142           } 
00143           if(m_bWriteMax){
00144             printf("max[%s]  ",getTimeStr(m_liMaxTime).c_str());
00145           }
00146           printf("{%s}\n",m_sFunctionName.c_str());
00147         }
00149         void incCount(){
00150           m_liCount++;
00151         }
00153         void incTime(long int dt){
00154           m_liTime+=dt;
00155           m_liMinTime = iclMin(dt,m_liMinTime);
00156           m_liMaxTime = iclMax(dt,m_liMaxTime);
00157         }
00158         private:
00159         long int m_liCount;
00160         long int m_liTime;
00161         long int m_liMaxTime;
00162         long int m_liMinTime;
00163         std::string m_sFunctionName;
00164   
00165         bool m_bWriteCounts;
00166         bool m_bWriteAvg;
00167         bool m_bWriteMin;
00168         bool m_bWriteMax;
00169       };
00171       StackTimer(StackTimerNotifier *notifier){
00172         m_poTimer = new Timer(1);//1=ns
00173         m_poNotifier = notifier;
00174         notifier->incCount();
00175         m_poTimer->start();
00176       }
00178       ~StackTimer(){
00179         m_poNotifier->incTime(m_poTimer->stopSilent());
00180         delete m_poTimer;
00181       }
00182       private:
00183       Timer *m_poTimer;
00184       StackTimerNotifier* m_poNotifier;
00185      
00186     };
00187     
00188   #define BENCHMARK_THIS_SECTION(SECTION_NAME)                            \
00189     static icl::utils::StackTimer::StackTimerNotifier __notifier(#SECTION_NAME);      \
00190     icl::utils::StackTimer __stacktimer(&__notifier);
00191     
00192   #define BENCHMARK_THIS_FUNCTION                                         \
00193     static icl::utils::StackTimer::StackTimerNotifier __notifier(__FUNCTION__);       \
00194     icl::utils::StackTimer __stacktimer(&__notifier);
00195     
00196   #define BENCHMARK_THIS_FUNCTION_LITE                                       \
00197     static icl::utils::StackTimer::StackTimerNotifier __notifier(__FUNCTION__,0,0,0,0);  \
00198     icl::utils::StackTimer __stacktimer(&__notifier);
00199   } // namespace utils
00200 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines