• <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>

            小明思考

            高性能服務器端計算
            posts - 70, comments - 428, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            C++性能優化實踐2---選擇合適的容器

            Posted on 2006-01-05 13:07 小明 閱讀(1508) 評論(1)  編輯 收藏 引用 所屬分類: C/C++
            有很多性能問題,在系統使用的初期,不大能看出來,因為使用量的很小。隨著系統的不斷深入使用,性能問題才出現,尤其是那種24*7都要不停運行的程序。

            下面的一個例子,是經常在項目中都會用到的.ini配置文件生成和解析的過程
            比如

            [section1]
               key1 = value1 ;here is comment
               key2 = value2
            [section2]
               key3 = value3
               key4 = value4

            當然WinAPI也提供了WritePrivateProfileString,GetPrivateProfileString兩個API來讀寫這種文件格式,但是
            每次使用都會打開文件來讀寫,性能非常低,只適用于小規模的數據讀寫。

            看我們的代碼:
            #define _TD_INI_H_

            #include 
            <list>
            #include 
            <fstream>
            using namespace std;
            class KEV_VALUE
            {
            public:
                    KEV_VALUE(){m_bIsGarbage
            =FALSE;m_bSingleLine=FALSE;};
                    
            virtual ~KEV_VALUE(){};
                    
            string m_key;
                    
            string m_value;
                    BOOL m_bIsGarbage;
                    BOOL m_bSingleLine;
            };
            typedef list
            <KEV_VALUE> LIST_KEY_VELUE;

            class SECTION
            {
            public:
                    SECTION(){};
                    
            virtual ~SECTION(){};
                    
            string m_section;
                    LIST_KEY_VELUE m_listKeyValue;
            };
            typedef list
            <SECTION> LIST_SECTION;

            class CTDIni  
            {
            public:
                    CTDIni( 
            void );
                    
            virtual ~CTDIni( void );
                    BOOL UpdateData( BOOL bSave
            =true );
                    
            void SetFileName( const CHAR *lpstrFileName );
                    BOOL GetLastSectionName( 
            string& str );
                    BOOL AddSection( 
            const CHAR *lpstrSection );
                    BOOL DeleteSection( 
            const CHAR *lpstrSection);
                    BOOL ReadSection( 
            const CHAR *lpszSection, string& str );        
                    BOOL SetKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey, const CHAR *lpstrValue );
                    BOOL SetKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey, const INT32 nValue );
                    BOOL GetKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey, CHAR *lpstrValue );
                    BOOL DeleteKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey );
                    BOOL ChangeKeyName( 
            const CHAR *lpstrSection, const CHAR *lpstrKeyOld, const CHAR *lpstrKeyNew );
                    BOOL ChangeSectionName( 
            const CHAR *lpstrSectionOld, const CHAR *lpstrSectionNew );
            private:
                    LIST_SECTION m_sections;
                    
            string m_strFileName;
                    BOOL AddGarbage( 
            const CHAR *lpstrSection, const CHAR *lpstrGarbage, BOOL bSingleLine=FALSE );
            };

            #endif // !defined(_TD_INI_H_)

            這個是解析類的聲明,重要的是它的數據結構,它采用了兩級鏈表的結構,第一級是所有section的list,第二級是section下面的key-value的list.
            下面是其中的實現代碼片斷

            BOOL CTDIni::SetKeyValue( const CHAR *lpstrSection, const CHAR *lpstrKey, const CHAR *lpstrValue )
            {
                    LIST_SECTION::iterator i;
                    
            for( i = m_sections.begin(); i != m_sections.end(); i++ )
                    {
                            
            if( (*i).m_section == lpstrSection )
                            {
                                    LIST_KEY_VELUE::iterator j;
                                    
            for( j = (*i).m_listKeyValue.begin(); j != (*i).m_listKeyValue.end(); j++ )
                                    {
                                            
            if( (*j).m_key == lpstrKey )
                                            {
                                                    (
            *j).m_value = lpstrValue;
                                                    
            return TRUE;
                                            }
                                    }

                                    KEV_VALUE tmp;
                                    tmp.m_key 
            = lpstrKey;
                                    tmp.m_value 
            = lpstrValue;
                                    (
            *i).m_listKeyValue.push_back( tmp );
                                    
            return TRUE;
                            }
                    }
                    
            return FALSE;
            }

            上面的一個方法是添加一個值到配置中去,它的算法是首先查找它所在的section,然后查找所在的key,
            最后決定是insert還是update.

            這樣性能問題就來了,當數據不斷增加,SetKeyValue所需要的時間呈N*N的方式增長。很可怕。CPU的占用率也會跑到很高。

            最后,我們不得不進行優化,因為在我們的項目中,不存在相同的section,也沒有相同的key,所以我們使用map,使得查找時間變成常數級別。(即使有相同的key&section,也可以使用multimap)

            優化后的數據結構是這樣的

            #include <string>
            #include 
            <stdio.h>
            #include 
            <list>
            #include 
            <map>
            #include 
            <fstream>

            using namespace std;

            struct VELUE
            {
                    
            string m_value;
                    BOOL m_bIsGarbage;
                    BOOL m_bSingleLine;
            };

            typedef std::map
            <std::string,VELUE> MAP_KEY_VELUE;

            typedef std::map
            <std::string,MAP_KEY_VELUE>  MAP_SECTION;

            class CTDIni  
            {
            public:
                    CTDIni( 
            void );
                    
            virtual ~CTDIni( void );

                    BOOL UpdateData( BOOL bSave
            =true );

                    
            void SetFileName( const CHAR *lpstrFileName );

                    BOOL GetLastSectionName( 
            string& str );

                    BOOL AddSection( 
            const CHAR *lpstrSection );
                    BOOL DeleteSection( 
            const CHAR *lpstrSection);
                    BOOL ReadSection( 
            const CHAR *lpszSection, string& str );        
                    BOOL IsExistSection( 
            const CHAR *lpstrSection);

                    BOOL SetKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey, const CHAR *lpstrValue );
                    BOOL SetKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey, const INT32 nValue );
                    BOOL GetKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey, CHAR *lpstrValue );
                    BOOL DeleteKeyValue( 
            const CHAR *lpstrSection, const CHAR *lpstrKey );
                    BOOL ChangeKeyName( 
            const CHAR *lpstrSection, const CHAR *lpstrKeyOld, const CHAR *lpstrKeyNew );
                    BOOL ChangeSectionName( 
            const CHAR *lpstrSectionOld, const CHAR *lpstrSectionNew );
            private:
                    MAP_SECTION m_sections;
                    
            string m_strFileName;
                    BOOL AddGarbage( 
            const CHAR *lpstrSection, const CHAR *lpstrGarbage, BOOL bSingleLine=FALSE );
            };


            SetKeyValue那個方法的實現是這樣:
            BOOL CTDIni::SetKeyValue( const CHAR *lpstrSection, const CHAR *lpstrKey, const CHAR *lpstrValue )
            {
                    MAP_SECTION::iterator i;
                    MAP_KEY_VELUE::iterator j;

                    i 
            = m_sections.find(lpstrSection);
                    
            if ( i == m_sections.end())
                    {
                            
            return FALSE;
                    }

                    j 
            = i->second.find(lpstrKey);
                    
            if( j != i->second.end()) //update
                    {
                            j
            ->second.m_value = lpstrValue;
                    }
                    
            else //insert
                    {
                            VELUE tmp;
                            tmp.m_value 
            = lpstrValue;
                            tmp.m_bSingleLine 
            = false;
                            tmp.m_bIsGarbage 
            = false;
                            i
            ->second[lpstrKey] = tmp;
                    }
                    
            return TRUE;
            }

            兩者的性能差距有多大?超過你的想象

            Feedback

            # re: C++性能優化實踐2---選擇合適的容器  回復  更多評論   

            2006-01-07 10:33 by 力為
            Ogre中的配置文件基本上也是按照這種方式組織的。看來在實際項目中std::map的作用大的很。:)
            久久99国产综合精品| 香蕉99久久国产综合精品宅男自| 久久狠狠爱亚洲综合影院| 精品国产乱码久久久久软件| 少妇人妻综合久久中文字幕| 久久久婷婷五月亚洲97号色| 久久精品国产91久久麻豆自制| 久久久久久极精品久久久| 亚洲狠狠婷婷综合久久久久| 中文字幕一区二区三区久久网站| 伊人伊成久久人综合网777| 久久久91精品国产一区二区三区| 日韩精品无码久久一区二区三| 国产精品美女久久久久久2018| 国产ww久久久久久久久久| 亚洲国产精品无码久久久蜜芽 | 久久综合亚洲色一区二区三区| 人妻精品久久久久中文字幕69| 久久久久噜噜噜亚洲熟女综合 | 国产精品女同久久久久电影院| 久久精品国产亚洲5555| 久久国产精品99国产精| 一97日本道伊人久久综合影院| 国产成人综合久久久久久| 精品蜜臀久久久久99网站| 77777亚洲午夜久久多喷| 国内精品久久久久久久久| 国内精品伊人久久久久AV影院| 亚洲欧美成人久久综合中文网 | 伊人色综合久久天天网| 精品国产乱码久久久久久浪潮 | 久久精品视频一| 色综合久久久久综合99| 91精品国产91久久久久久| 久久综合狠狠色综合伊人| 国产69精品久久久久777| 久久永久免费人妻精品下载| a高清免费毛片久久| 精品久久久久久亚洲| 久久久综合九色合综国产| 99久久中文字幕|