• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            歲月流轉,往昔空明

            C++博客 首頁 新隨筆 聯系 聚合 管理
              118 Posts :: 3 Stories :: 413 Comments :: 0 Trackbacks

             

            struct Timer{
                
            void GetTime(__int64* ptime)
                
            {
                    
            //QueryPerformanceCounter((LARGE_INTEGER*)ptime);
                    __asm
                    
            {
                        push edx;
                        push ecx;
                        mov ecx,ptime;
                        rdtsc
                            mov [ecx],eax;
                        mov [ecx
            +4],edx;
                        pop ecx;
                        pop edx;
                    }

                }

            }
            ;

            Timer g_timer;

            struct ProfilerSample
            {
                __int64 begin;
                __int64 end;
                __int64 count;
                __int64 total;

                ProfilerSample() : begin(
            0), end(0), count(0), total(0)
                
            {}

                
            void Reset() {
                    begin 
            = end = count = total = 0;
                }


                
            void Begin() {
                    g_timer.GetTime(
            &begin);
                }


                
            void End() {
                    g_timer.GetTime(
            &end);
                    total 
            += (end - begin);
                }

            }
            ;

            class ProfilerNode
            {
            public:
                ProfilerNode
            * m_parent;
                ProfilerNode
            * m_son;
                ProfilerNode
            * m_sibling;

                
            const char* m_profName;

                ProfilerSample m_sample;

            public:
                ProfilerNode(
            const char* name, ProfilerNode* parent);
                
            ~ProfilerNode();

                
            void Begin();
                
            void End();

                
            const char* GetName();
                
            const ProfilerSample* GetProfSample();

                ProfilerNode
            * GetSon(const char* str);
                ProfilerNode
            * GetDirectSon();
                ProfilerNode
            * GetNextSibling();
                ProfilerNode
            * GetParent();
            }
            ;

            ProfilerNode::ProfilerNode(
            const char* name, ProfilerNode* parent)
                : m_profName(name), m_parent(parent), m_son(NULL), m_sibling(NULL)
            {
            }


            ProfilerNode::
            ~ProfilerNode()
            {
                delete m_son;
                delete m_sibling;
            }


            const char* ProfilerNode::GetName()
            {
                
            return m_profName;
            }


            const ProfilerSample* ProfilerNode::GetProfSample()
            {
                
            return &m_sample;
            }


            //獲取一個兒子
            ProfilerNode* ProfilerNode::GetSon(const char* str)
            {
                ProfilerNode
            * p = m_son;

                
            while(p != NULL) {
                    
            if(p->m_profName == str) {
                        
            break;
                    }

                    p 
            = p->GetNextSibling();
                }


                
            if(p == NULL)
                
            {
                    p 
            = new ProfilerNode(str, this);
                    p
            ->m_sibling = m_son;
                    m_son 
            = p;
                }


                
            return p;
            }


            //獲取一個兒子
            ProfilerNode* ProfilerNode::GetDirectSon()
            {
                
            return m_son;
            }


            ProfilerNode
            * ProfilerNode::GetNextSibling()
            {
                
            return m_sibling;
            }


            ProfilerNode
            * ProfilerNode::GetParent()
            {
                
            return m_parent;
            }


            void ProfilerNode::Begin()
            {
                m_sample.Begin();
            }


            void ProfilerNode::End()
            {
                m_sample.End();
            }


            //性能分析管理器
            //注意初始化順序!
            class ProfilerManager
            {
            private:
                ProfilerManager() : root(
            "root", NULL), m_curProfNode(&root){}
                ProfilerManager(
            const ProfilerManager& rhs);
                ProfilerManager
            &  operator = (const ProfilerManager& rhs);

                
            static ProfilerManager s_profmgr;

            private:
                ProfilerNode root;
                ProfilerNode
            * m_curProfNode;

            public:
                
            static ProfilerManager& Instance() {
                    
            return s_profmgr;
                }


                
            void BeginProfiler(const char* name);
                
            void EndProfiler();

                ProfilerNode
            * GetRootProfilerNode() {
                    
            return &root;
                }

            }
            ;

            ProfilerManager ProfilerManager::s_profmgr;

            void ProfilerManager::BeginProfiler(const char* name)
            {
                m_curProfNode 
            = m_curProfNode->GetSon(name);
                m_curProfNode
            ->Begin();
            }


            void ProfilerManager::EndProfiler()
            {
                m_curProfNode
            ->End();
                m_curProfNode 
            = m_curProfNode->GetParent();
            }


            class Profiler
            {
            public:
                Profiler(
            const char* name)
                
            {
                    ProfilerManager::Instance().BeginProfiler(name);
                }

                
            ~Profiler()
                
            {
                    ProfilerManager::Instance().EndProfiler();
                }

            }
            ;

            #define BEGIN_PROF(name) ProfilerManager::Instance().BeginProfiler(name);
            #define END_PROF(name) ProfilerManager::Instance().EndProfiler();

            LARGE_INTEGER j;

            void OutputProf(ProfilerNode* p, int space = 0)
            {
                ProfilerNode
            * tmp;

                
            const ProfilerSample* ps = p->GetProfSample();
                
            for(int i = 0; i < space; ++i) {
                    cout 
            << " ";
                }

                cout 
            << p->GetName() << ": 調用次數: " << ps->count << " 調用總耗時: " << ps->total << endl;

                
            if(tmp = p->GetDirectSon()) {
                    OutputProf(tmp, space
            +1);
                }
             else {
                    
            if(tmp = p->GetNextSibling()) {
                        OutputProf(tmp, space);
                    }

                }

            }
            posted on 2005-11-30 11:32 空明流轉 閱讀(1500) 評論(0)  編輯 收藏 引用
            99久久久久| 久久亚洲熟女cc98cm| 日韩欧美亚洲综合久久| 狠狠久久综合| 无码国产69精品久久久久网站| 久久精品免费一区二区| 99久久婷婷免费国产综合精品| 久久99免费视频| 国产欧美久久久精品影院| 亚洲а∨天堂久久精品| 国产V亚洲V天堂无码久久久| 九九久久精品无码专区| 久久久中文字幕日本| 久久精品99久久香蕉国产色戒| 性高朝久久久久久久久久| 久久久噜噜噜久久熟女AA片| 精品久久一区二区| 亚洲精品国产第一综合99久久 | 天天爽天天狠久久久综合麻豆 | 99久久无码一区人妻a黑| 日本精品久久久久中文字幕| 天天躁日日躁狠狠久久| 欧美综合天天夜夜久久| 久久精品国产色蜜蜜麻豆| 青草影院天堂男人久久| 久久国产精品成人影院| 久久久黄色大片| 国产成人AV综合久久| 情人伊人久久综合亚洲| 久久午夜免费视频| 久久精品亚洲乱码伦伦中文| 伊人热人久久中文字幕| 久久妇女高潮几次MBA| 久久久综合香蕉尹人综合网| 日韩欧美亚洲综合久久影院d3| 亚洲精品无码久久久久sm| 亚洲精品国产第一综合99久久| 精品久久国产一区二区三区香蕉| 久久99国产乱子伦精品免费| 久久中文字幕精品| 国产精品久久久久久久人人看|