- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/src/frost_utils_profiler.cpp

Go to the documentation of this file.

    00001 /* ###################################### */
    00002 /* ###     Frost Engine, by Kalith    ### */
    00003 /* ###################################### */
    00004 /*             Profiler source            */
    00005 /*                                        */
    00006 /*                                        */
    00007
    00008 #include "frost_utils_profiler.h"
    00009 #include "frost_utils_timemanager.h"
    00010 #include "frost_utils_stdhelper.h"
    00011 #include "frost_utils_log.h"
    00012
    00013 using namespace std;
    00014
    00015 namespace Frost
    00016 {
    00017     const s_str Profiler::CLASS_NAME = "Profiler";
    00018     const s_str Chrono::CLASS_NAME = "Chrono";
    00019
    00020     Profiler::Profiler()
    00021     {
    00022     }
    00023
    00024     Profiler::Profiler( const s_uint& uiGroup, const s_str& sName, const s_bool& bRecord )
    00025     {
    00026         sName_ = sName;
    00027         uiGroup_ = uiGroup;
    00028         bRecord_ = bRecord;
    00029         uiCallNbr_ = 0;
    00030         uiZeroTimings_ = 0;
    00031         dTotalTime_ = 0.0f;
    00032         dLowestTime_ = -1.0f;
    00033         dHighestTime_ = -1.0f;
    00034     }
    00035
    00036     void Profiler::AddTiming( const s_double& dTime )
    00037     {
    00038         ++uiCallNbr_;
    00039
    00040         if (dTime < 0.000001)
    00041             ++uiZeroTimings_;
    00042         else
    00043         {
    00044             if ( (dLowestTime_ == -1.0) || (dTime < dLowestTime_) )
    00045                 dLowestTime_ = dTime;
    00046             if ( (dHighestTime_ == -1.0) || (dTime > dHighestTime_) )
    00047                 dHighestTime_ = dTime;
    00048
    00049             dTotalTime_ += dTime;
    00050
    00051             if (bRecord_)
    00052                 lRecordStack_.push_back(dTime);
    00053         }
    00054     }
    00055
    00056     void Profiler::SetKeepRecords( const s_bool& bRecord )
    00057     {
    00058         bRecord_ = bRecord;
    00059     }
    00060
    00061     void Profiler::PrintProfile( const s_double& dProfileTime )
    00062     {
    00063         Log("  - "+ sName_ +" :");
    00064         Log("     - call number     : "+ uiCallNbr_ +" ("+ uiZeroTimings_ +" not timed)");
    00065
    00066         if (uiCallNbr_ != 0)
    00067         {
    00068             Log("     - total time      : "+ s_str::Convert(dTotalTime_, 4) +" s ("+ s_str::Convert(100.0*dTotalTime_/dProfileTime, 2, 2) +"% of total)");
    00069             Log("     - average time    : "+ s_str::Convert(1000.0*dTotalTime_/s_double(uiCallNbr_-uiZeroTimings_), 4) +" ms");
    00070             Log("     - highest time    : "+ s_str::Convert(1000.0*dHighestTime_, 4) +" ms");
    00071             Log("     - lowest time     : "+ s_str::Convert(1000.0*dLowestTime_, 4) +" ms");
    00072
    00073             if (bRecord_)
    00074             {
    00075                 Log("     - listing records :");
    00076                 s_uint i = 1;
    00077                 vector<s_double>::iterator iter;
    00078                 foreach (iter, lRecordStack_)
    00079                 {
    00080                     Log("        ["+ i +"] : "+ 1000.0*(*iter) +" ms");
    00081                     ++i;
    00082                 }
    00083             }
    00084         }
    00085     }
    00086
    00087     const s_uint& Profiler::GetGroup()
    00088     {
    00089         return uiGroup_;
    00090     }
    00091
    00092     Chrono::Chrono(s_ptr<Profiler> pProfiler, const s_bool& bStart)
    00093     {
    00094         if (bStart)
    00095         {
    00096             if (pProfiler)
    00097                 dStart_ = TimeManager::GetSingleton()->GetTime();
    00098             bStopped_ = false;
    00099         }
    00100         else
    00101         {
    00102             bStopped_ = true;
    00103         }
    00104         pParent_ = pProfiler;
    00105         dTotal_ = 0.0f;
    00106     }
    00107
    00108     Chrono::~Chrono()
    00109     {
    00110         if ( pParent_ && (!bStopped_) )
    00111         {
    00112             s_double dTime = TimeManager::GetSingleton()->GetTime() - dStart_;
    00113             dTotal_ += dTime;
    00114             pParent_->AddTiming(dTotal_);
    00115         }
    00116     }
    00117
    00118     void Chrono::Start()
    00119     {
    00120         if (bStopped_)
    00121         {
    00122             if (pParent_)
    00123             {
    00124                 dStart_ = TimeManager::GetSingleton()->GetTime();
    00125             }
    00126             bStopped_ = false;
    00127         }
    00128     }
    00129
    00130     void Chrono::Stop( const s_bool& bSend )
    00131     {
    00132         if (!bStopped_)
    00133         {
    00134             if (pParent_)
    00135             {
    00136                 s_double dTime = TimeManager::GetSingleton()->GetTime() - dStart_;
    00137                 dTotal_ += dTime;
    00138                 if (!bSend)
    00139                 {
    00140                     pParent_->AddTiming(dTotal_);
    00141                     dTotal_ = 0.0;
    00142                 }
    00143             }
    00144             bStopped_ = true;
    00145         }
    00146     }
    00147
    00148     s_double Chrono::GetTime()
    00149     {
    00150         return TimeManager::GetSingleton()->GetTime() - dStart_;
    00151     }
    00152 }