• <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 小明 閱讀(1510) 評論(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的作用大的很。:)
            精品综合久久久久久888蜜芽| 久久精品国产亚洲沈樵| 亚洲国产精品无码久久青草| 四虎国产精品免费久久| 久久婷婷五月综合97色直播| 无码国内精品久久综合88| 99精品久久精品一区二区| 久久WWW免费人成—看片| 久久亚洲AV成人无码国产| 老司机国内精品久久久久| 久久久久亚洲AV无码观看| 久久99久久成人免费播放| 婷婷综合久久中文字幕蜜桃三电影| 欧美日韩中文字幕久久久不卡| 久久精品国产亚洲AV香蕉| 欧美久久精品一级c片片| 国产精品99久久久久久宅男小说| 99久久99这里只有免费费精品| 久久99精品久久久久久9蜜桃| 亚洲色婷婷综合久久| 久久无码AV中文出轨人妻 | 久久无码一区二区三区少妇| 亚洲AV无一区二区三区久久| 久久久噜噜噜久久| 久久久99精品成人片中文字幕| 久久午夜羞羞影院免费观看| 久久久久久精品久久久久| 四虎亚洲国产成人久久精品| 久久国产精品久久久| 99久久精品午夜一区二区| 亚洲国产另类久久久精品小说| 亚洲国产天堂久久久久久 | 久久亚洲美女精品国产精品| 日批日出水久久亚洲精品tv| 午夜天堂av天堂久久久| 久久成人国产精品| 国产成人精品久久二区二区| 亚洲欧洲精品成人久久奇米网| 国产成人精品久久亚洲高清不卡| 99久久成人国产精品免费| 久久亚洲综合色一区二区三区|