00001 00011 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00012 #pragma once 00013 #endif 00014 #ifndef COMMON_PROFILER_H_ 00015 #define COMMON_PROFILER_H_ 00016 00017 #include <stack> // :( 00018 00019 namespace common 00020 { 00021 00026 00027 class Profiler; 00028 00030 class ProfilerItem 00031 { 00032 private: 00033 // Sumaryczny czas wykonania [s] 00034 double m_Time; 00035 // Liczba przebiegów 00036 double m_Count; 00037 // Czas rozpoczęcia bieżącego przebiegu [s] 00038 double m_StartTime; 00039 tstring m_strName; // Nazwa elementu 00040 std::vector<ProfilerItem> m_ItemVector; // Podelementy 00041 00042 friend class Profiler; 00043 ProfilerItem* Begin(const tstring &Name); 00044 void Start(); 00045 void Stop(); 00046 public: 00047 ProfilerItem(const tstring &Name); 00048 00049 const tstring &GetName() { return m_strName; } 00050 bool Empty() { return (around(m_Count, 0.1)); } 00051 double GetCount() { return m_Count; } 00052 double GetAvgTime() { return ( Empty() ? 0.0 : m_Time / m_Count ); } 00053 size_t GetItemCount() { return m_ItemVector.size(); } 00054 ProfilerItem* GetItem(size_t index) { return &m_ItemVector[index]; } 00055 void FormatString(tstring *S, unsigned dwLevel); 00056 }; 00057 00059 class Profiler 00060 { 00061 private: 00062 std::stack<ProfilerItem*> m_ItemStack; 00063 ProfilerItem m_DefaultItem; 00064 00065 public: 00066 Profiler(); 00067 Profiler(const tstring &Name); 00068 void Begin(const tstring &Name); 00069 void End(); 00070 ProfilerItem* GetRootItem() { return &m_DefaultItem; } 00072 00075 void FormatString(tstring *S); 00076 }; 00077 00079 00084 class Profile 00085 { 00086 private: 00087 Profiler &m_Profiler; 00088 public: 00090 Profile(const tstring &Name); 00092 Profile(const tstring &Name, Profiler &Profiler_); 00093 ~Profile(); 00094 }; 00095 00097 extern Profiler g_Profiler; 00098 00100 // code_profiler 00101 00102 } // namespace common 00103 00109 #define PROFILE_GUARD(Name) common::Profile __profile_guard_object(Name); 00110 00111 00112 #endif 00113
1.5.5