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

            小明思考

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

            C++性能優(yōu)化實踐2---選擇合適的容器

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

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

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

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

            看我們的代碼:
            #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_)

            這個是解析類的聲明,重要的是它的數(shù)據(jù)結(jié)構(gòu),它采用了兩級鏈表的結(jié)構(gòu),第一級是所有section的list,第二級是section下面的key-value的list.
            下面是其中的實現(xiàn)代碼片斷

            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.

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

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

            優(yōu)化后的數(shù)據(jù)結(jié)構(gòu)是這樣的

            #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那個方法的實現(xiàn)是這樣:
            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++性能優(yōu)化實踐2---選擇合適的容器  回復(fù)  更多評論   

            2006-01-07 10:33 by 力為
            Ogre中的配置文件基本上也是按照這種方式組織的??磥碓趯嶋H項目中std::map的作用大的很。:)
            久久久久久综合一区中文字幕| 97久久国产露脸精品国产| 思思久久精品在热线热| 久久综合九色综合久99| 精品国产一区二区三区久久久狼| 久久午夜夜伦鲁鲁片免费无码影视 | 国产综合精品久久亚洲| 性做久久久久久久| 国产精品九九久久免费视频| 精品国产热久久久福利| 久久综合久久鬼色| 久久综合一区二区无码| 久久久久99这里有精品10| 久久人人青草97香蕉| 久久婷婷五月综合97色一本一本| 亚洲va中文字幕无码久久| 2020久久精品国产免费| 亚洲欧洲久久久精品| 久久人人爽人人爽人人爽 | 久久精品一本到99热免费| 精品久久无码中文字幕| 亚洲国产日韩欧美综合久久| 无码人妻久久一区二区三区免费丨| 久久精品一区二区| 丁香色欲久久久久久综合网| 国产精品成人99久久久久 | 久久天天躁夜夜躁狠狠| 国产精品热久久毛片| 一级做a爰片久久毛片人呢| 久久精品国产久精国产果冻传媒| 久久免费高清视频| 精品久久久久久国产| 国产精品免费看久久久| 波多野结衣AV无码久久一区| 一本大道久久东京热无码AV| 国产伊人久久| 日韩欧美亚洲综合久久影院Ds| 久久天天躁狠狠躁夜夜不卡 | 国产A级毛片久久久精品毛片| 韩国免费A级毛片久久| 无码人妻久久一区二区三区免费|