LR-splines  0.5
Profiler.h
Go to the documentation of this file.
00001 // $Id: Profiler.h 1013 2011-05-26 14:39:16Z kmo $
00002 //==============================================================================
00012 //==============================================================================
00013 
00014 #ifndef _PROFILER_H
00015 #define _PROFILER_H
00016 
00017 #include <iostream>
00018 #include <string>
00019 #include <map>
00020 #include <sys/time.h>
00021 
00022 namespace LR {
00023 
00036 class Profiler
00037 {
00038 public:
00045   Profiler(const std::string& name);
00047   ~Profiler();
00048 
00050   void start(const std::string& funcName);
00052   void stop(const std::string& funcName);
00053 
00055   void report(std::ostream& os) const;
00057   void clear() { myTimers.clear(); allCPU = allWall = 0.0; nRunners = 0; }
00058 
00059 private:
00061   struct Profile
00062   {
00063     clock_t startCPU;  
00064     double  startWall; 
00065     double  totalCPU;  
00066     double  totalWall; 
00067     size_t  nCalls;    
00068     bool    running;   
00069 
00071     Profile() : nCalls(0), running(false) { totalCPU = totalWall = 0.0; }
00073     bool haveTime() const { return totalCPU >= 0.005 || totalWall >= 0.005; }
00074   };
00075 
00077   friend std::ostream& operator<<(std::ostream& os, const Profile& p);
00078 
00079   std::string myName; 
00080 
00081   std::map<std::string,Profile> myTimers; 
00082 
00083   double allCPU;  
00084   double allWall; 
00085 
00092   size_t nRunners; 
00093 };
00094 
00095 
00096 namespace utl
00097 {
00098   extern Profiler* profiler; 
00099 
00101   class prof
00102   {
00103     const char* name; 
00104   public:
00106     prof(const char* tag) : name(tag) { if (profiler) profiler->start(name); }
00108     ~prof() { if (profiler) profiler->stop(name); }
00109   };
00110 }
00111 
00112 
00114 #define PROFILE(label) utl::prof _prof(label)
00115 
00116 #if PROFILE_LEVEL >= 1
00117 #define PROFILE1(label) PROFILE(label)
00118 #else
00119 
00120 #define PROFILE1(label)
00121 #endif
00122 
00123 #if PROFILE_LEVEL >= 2
00124 #define PROFILE2(label) PROFILE(label)
00125 #else
00126 
00127 #define PROFILE2(label)
00128 #endif
00129 
00130 #if PROFILE_LEVEL >= 3
00131 #define PROFILE3(label) PROFILE(label)
00132 #else
00133 
00134 #define PROFILE3(label)
00135 #endif
00136 
00137 #if PROFILE_LEVEL >= 4
00138 #define PROFILE4(label) PROFILE(label)
00139 #else
00140 
00141 #define PROFILE4(label)
00142 #endif
00143 
00144 } // end namespace LR
00145 
00146 #endif