SourceForge.jp

dkutilTimer.h

説明を見る。
00001 
00008 #ifndef _dkutilTimer__
00009 #define _dkutilTimer__
00010 
00011 #include "dkutilCompileOption.h"
00012 #include <boost/progress.hpp>
00013 #if DKUTIL_COMPILE_OPTION_MSVC
00014 #   pragma comment(lib,"winmm")
00015 #   include "dkutilCPU.h"
00016 #endif
00017 
00018 
00019 namespace dkutil{
00020 
00021 namespace policy{
00022 
00023 struct ReportBase{
00024     enum{
00025         enuCharBuffSize = 1024,
00026     };
00027 };
00028 
00029 struct ConsoleReport : public ReportBase
00030 {
00031   static void Output(double t,const char *f,char *str,...) {
00032         char s[enuCharBuffSize]="";
00033         SET_VA_LIST_INSERT_YEN_N(s,sizeof(s),str);
00034         printf(s); 
00035     }
00036 };
00037 struct OutputDebugStringReport : public ReportBase{
00038     static void Output(double t,const char *f,const char *str,...) { 
00039         char s[enuCharBuffSize]="";
00040         SET_VA_LIST_INSERT_YEN_N(s,sizeof(s),str);
00041         OutputDebugString(s);
00042     }
00043 };
00044 struct LogFileReport : public ReportBase{
00045     static void Output(double t,const char *f,char *str,...) {
00046         char s[enuCharBuffSize]="";
00047         SET_VA_LIST_INSERT_YEN_N(s,sizeof(s),str);
00048         if(f){
00049             dkutil::dAddLog(f,s);
00050         }
00051     }
00052 };
00053 
00054 }//end of policy namespace
00055 
00056 
00057 
00058 template<class YOUR_CLASS,class OutputPolicy=policy::OutputDebugStringReport>
00059 class progress_base : public YOUR_CLASS{
00060     OutputPolicy mPolicy;
00061     char mFileName[MAX_PATH];
00062 public:
00063     explicit    progress_base(char *filename="debug.txt"){
00064         SetFileName(filename);
00065         restart();
00066     }
00067     virtual ~progress_base(){
00068         double time = elapsed();
00069         mPolicy.Output(time,GetFileName(),GetOutputSignature(),time);
00070     }
00071     OutputPolicy &GetOutputPolicy(){return mPolicy;}
00072     const char *GetFileName()const {return mFileName;}
00073     bool SetFileName(char *s){
00074         return DKUTIL_SUCCEEDED(dkstrcpy(mFileName,sizeof(mFileName),s,strlen(s)));
00075     }
00076 };
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 #ifdef _MSC_VER
00086 
00087 
00088 #pragma warning(disable:4035)
00089 
00090 //#if 0
00096 class RealCPUClock  : private boost::noncopyable{
00097     CPUInfo info;
00098     bool mIsTSC;
00099     typedef ULONGLONG VAR_TYPE;
00100     VAR_TYPE mStartTime;
00101 public:
00102 
00103     inline VAR_TYPE __fastcall RDTSC( )const
00104     {
00105         _asm{
00106             rdtsc
00107     //   _emit 0xf    ← rdtsc でエラーが出る古いバージョン
00108     //   _emit 0x31   ← の環境のとき
00109         }
00110     }
00111 
00112 
00113 
00114     /*
00115     inline ULONGLONG  RDTSC()const{
00116         typedef VAR_TYPE __fastcall (*RDTSC_TYPE)();
00117         RDTSC_TYPE t;
00118         static const BYTE rdtsc[]={
00119             0x55                                     //push             ebp
00120             ,0x8B,0xEC                              //mov       ,0x ebp,esp
00121             ,0x83,0xEC,0x44                      //sub               esp,44h
00122             ,0x53                                    //push             ebx
00123             ,0x56                                // push                esi
00124             ,0x57                                // push                edi
00125             ,0x51                               //   push               ecx
00126             ,0x8D,0x7D,0xBC              //     lea              edi,[ebp-44h]
00127             ,0xB9,0x11,0x00,0x00,0x00       //   mov                 ecx,11h
00128             ,0xB8,0xCC,0xCC,0xCC,0xCC       //   mov                 eax,0CCCCCCCCh
00129             ,0xF3,0xAB                       //  rep stos       dword ptr [edi]
00130             ,0x59                               //   pop                 ecx
00131             ,0x89,0x4D,0xFC                 //   mov                 dword ptr [ebp-4],ecx
00132             ,0x0F,0x31                       //  rdtsc
00133             ,0x5F                               //   pop                 edi
00134             ,0x5E                               //   pop                 esi
00135             ,0x5B                               //   pop                 ebx
00136             ,0x83,0xC4,0x44                 //   add                 esp,44h
00137             ,0x3B,0xEC                       //  cmp                 ebp,esp
00138             ,0xE8,0x14,0x34,0x00,0x00       //   call               __chkesp (00405fb0)
00139             ,0x8B,0xE5                       //  mov                 esp,ebp
00140             ,0x5D                               //   pop                 ebp
00141             ,0xC3                               //   ret
00142         };
00143         t = (RDTSC_TYPE)&rdtsc;
00144         ULONGLONG l=t();
00145         return l;
00146     }
00147     */
00148     inline VAR_TYPE get_clock_()const{  return RDTSC();}
00149     inline VAR_TYPE calc_clock_(VAR_TYPE c)const{ return RDTSC() - c;}
00150 /*
00151     VAR_TYPE get_clock_()const{
00152         VAR_TYPE CPUClock=0;    
00153         {
00154             __asm
00155             {
00156                 RDTSC
00157                 MOV     [CPUClock],     EAX
00158             }
00159         }
00160         return CPUClock;
00161     }
00162     VAR_TYPE calc_clock_(VAR_TYPE c)const{
00163         VAR_TYPE Now;
00164         __asm
00165         {
00166             RDTSC
00167             SUB     EAX,            [Now]
00168             MOV     [Now],      EAX
00169         }
00170         return Now - c;
00171     }
00172 */
00173     RealCPUClock(){
00174         mIsTSC = info.isTSC();
00175         mStartTime = 0;
00176         restart();
00177     }
00178     void restart() { 
00179         if(mIsTSC==false){
00180             throw std::logic_error("CPUTimer:CPUのTSCが使えない");
00181         }
00182         mStartTime = get_clock_();
00183     }
00184   double elapsed() const {  
00185         return double((LONGLONG)calc_clock_(mStartTime));
00186     }
00187     const char *GetOutputSignature(){
00188         static const char *ptr="RealCPUClock:%f\n";
00189         return ptr;
00190     }
00191 };//timer
00192 
00193 #pragma warning(default:4035)
00194 
00195 
00197 template<class ReportPolicy=policy::OutputDebugStringReport>
00198 struct ProgressRealCPUClockTiemr : public progress_base< RealCPUClock,ReportPolicy>{
00199     typedef progress_base< RealCPUClock,ReportPolicy> BASE_TYPE;
00200     ProgressRealCPUClockTiemr(char *filename="debug.txt") : BASE_TYPE(filename)
00201     {}
00202     virtual ~ProgressRealCPUClockTiemr(){}
00203 };
00204 //#endif //end of if 0
00205 #endif //end of _MSC_VER
00206 
00207 
00208 
00209 class MilliSecondClock  : private boost::noncopyable{
00210     DWORD mStartTime;
00211 public:
00212     MilliSecondClock(){
00213         mStartTime = 0;
00214         restart();
00215     }
00216     void restart() { 
00217         mStartTime = ::timeGetTime();
00218     }
00219   double elapsed() const {  
00220         return (double)::timeGetTime() - mStartTime;
00221     }
00222     const char *GetOutputSignature(){
00223         static const char *ptr="MilliSecondClock:%f\n";
00224         return ptr;
00225     }
00226 };
00227 
00228 struct BoostTimerImpl : public boost::timer{
00229     const char *GetOutputSignature(){
00230         static const char *ptr="boost::timer(std::clock()):%f\n";
00231         return ptr;
00232     }
00233 };
00234 
00236 template<class ReportPolicy=policy::OutputDebugStringReport>
00237 struct ProgressClockTiemr : public progress_base<BoostTimerImpl,ReportPolicy>{
00238     typedef progress_base<BoostTimerImpl,ReportPolicy> BASE_TYPE;
00239     ProgressClockTiemr(char *filename="debug.txt") : BASE_TYPE(filename)
00240     {}
00241 };
00242 
00244 template<class ReportPolicy=policy::OutputDebugStringReport>
00245 struct ProgressMilliSecondTiemr : public progress_base<MilliSecondClock,ReportPolicy>{
00246     typedef progress_base<MilliSecondClock,ReportPolicy> BASE_TYPE;
00247     ProgressMilliSecondTiemr(char *filename="debug.txt") : BASE_TYPE(filename)
00248     {}
00249     virtual ~ProgressMilliSecondTiemr(){}
00250 };
00251 
00252 
00253 template<class T>
00254 struct ProcessTimer{
00255     //local_singleton<T> mSgtn;
00256     T *mPtr;
00257     ProcessTimer(){mPtr=NULL;}
00258     virtual ~ProcessTimer(){
00259         end();
00260     }
00261     void begin(){
00262         if(mPtr){
00263         }else{
00264             mPtr = new T;
00265         }
00266     
00267         /*
00268         if(mSgtn.isNull()){
00269             mSgtn.CheckInstance();
00270         }else{
00271             DEBUGMB("Don't call twice or more.");
00272         }
00273         */
00274     }
00275     void end(){
00276         if(mPtr){
00277             delete mPtr;
00278             mPtr=NULL;
00279         }
00280         //mSgtn.Release();
00281     }
00282 };
00283 
00284 
00285 #define BEGIN_DKINGYO_CPUCLOCK_DOUT() { \
00286     dkutil::progress_base<boost::timer> *p_dkingyocpu = new progress_base<boost::timer>
00287 
00288 #define END_DKINGYO_CPUCLOCK_DOUT() delete p_dkingyocpu; }
00289 
00290 #define BEGIN_DKINGYO_CPUCLOCK_LOG(s) { \
00291 dkutil::progress_base<boost::timer,policy::LogFileReport> *p_dkingyocpu = \
00292         new dkutil::progress_base<boost::timer,policy::LogFileReport>(s)
00293 
00294 #define END_DKINGYO_CPUCLOCK_LOG() delete p_dkingyocpu; }
00295 
00296 
00297 }//end of dkutil namespace
00298 
00299 
00300 #endif //end of include once

dkutil 1.02リリース前 d金魚専用マニュアルバージョンに対してSun Dec 28 21:23:08 2003に生成されました。 doxygen 1.3.5