• <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 - 7,  comments - 64,  trackbacks - 0

            記得以前在一本書上看過boost::shared_ptr的回帶來一定的效率損失,但是并不大.今天閑來無事,編譯了一個(gè)BOOST并簡(jiǎn)單測(cè)試了一下,看看到底有多少性能損失.測(cè)試代碼和結(jié)果如下:

            比較函數(shù)
            template <class T>
            class compareP
            {
            public:
                    bool operator() (const T lh,const T rh) const
                    {
                            return *lh<*rh;
                    }

            };


            boost:
            int _tmain(int argc, _TCHAR* argv[])
            {
                    DWORD oldtime = GetTickCount();
                    typedef map<boost::shared_ptr<string>,size_t,compareP<boost::shared_ptr<string> > > container_type;
                    typedef container_type::iterator iterator;
                    container_type container;
                    for (size_t i(0) ; i<500000 ; ++i)
                    {
                            boost::shared_ptr<string> pstr(new string);
                            char buffer[32];
                            *pstr = itoa(i,buffer,10);
                            container[pstr]=0;
                    }
                    cout<<GetTickCount() - oldtime <<endl;
            return EXIT_SUCCESS;
            }

            boost結(jié)果:
            2000
            2015
            2015
            2016
            2017


            指針:
            int _tmain(int argc, _TCHAR* argv[])
            {
                    DWORD oldtime = GetTickCount();
                    typedef map<string*,size_t,compareP<string*> > container_type;
                    typedef container_type::iterator iterator;
                    container_type container;
                    for (size_t i(0) ; i<500000 ; ++i)
                    {
                            string *pstr = new string;
                            char buffer[32];
                            *pstr = itoa(i,buffer,10);
                            container[pstr]=0;
                    }
                    cout<<GetTickCount() - oldtime <<endl;
            return EXIT_SUCCESS;
            }

            指針結(jié)果:
            937
            938
            954
            953
            953

            執(zhí)行速度已經(jīng)相差一倍了.自動(dòng)內(nèi)存管理的代價(jià)啊!~~~~
            環(huán)境:Q8200 2.33GHZ 4G內(nèi)存

            以上只是簡(jiǎn)單的測(cè)試,僅拱參考


            補(bǔ)充說明:

            很多朋友流言說在MAP中存放string*沒有意義。可是我上一個(gè)項(xiàng)目就需要這么做。

            試想我現(xiàn)在有10篇文檔,現(xiàn)在需要統(tǒng)計(jì)每篇文檔當(dāng)中每個(gè)字出現(xiàn)的次數(shù),還需要統(tǒng)計(jì)全部文章中全部字出現(xiàn)的次數(shù)。為了保證效率,應(yīng)當(dāng)保證每個(gè)字在內(nèi)存當(dāng)中只留一份拷貝(因?yàn)橐院笥锌赡芙y(tǒng)計(jì)每個(gè)詞,甚至每句話出現(xiàn)的次數(shù))。要實(shí)現(xiàn)這個(gè)功能,是否還有更好的算法?


            還有朋友流言說指針版沒有銷毀string指針。可是在這個(gè)程序中執(zhí)行cout<<GetTickCount() - oldtime <<endl;之前shared_ptr也沒有釋放資源。所以資源的釋放不會(huì)造成誤差。

            luck朋友的方法最為有效,把比較函數(shù)變成:

            bool operator() (const T &lh,const T &rh) const

                    {

                            return *lh<*rh;

                    }

            下面是shared_ptr執(zhí)行5次的時(shí)間:

            968

            969

            985

            969

            969


            下面是string*執(zhí)行5次的時(shí)間:

            859

            875

            860

            859

            860


            看來即使是小對(duì)象也不能放松!當(dāng)很多小對(duì)象發(fā)生構(gòu)造和析構(gòu)時(shí)所耗費(fèi)的時(shí)間還是不容小視的!~

            posted on 2009-06-30 21:09 HIT@ME 閱讀(3808) 評(píng)論(14)  編輯 收藏 引用

            FeedBack:
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-06-30 21:35 | skyscribe
            你舉的這個(gè)例子不具有典型性,實(shí)際中寫出這樣的代碼,應(yīng)該是需要再好好斟酌,誰會(huì)把shared_ptr作為map的索引呢?
            應(yīng)該舉一個(gè)更切合實(shí)際的例子,譬如map的第二個(gè)字段是shared_ptr類型。
            另外你的代碼有沒有把所有的優(yōu)化都用上,強(qiáng)迫inline展開?  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-06-30 23:31 | HIT@ME
            我最近做的一個(gè)項(xiàng)目就需要把string*作為map的索引(比如統(tǒng)計(jì)文章中重復(fù)出現(xiàn)的單詞數(shù))。另外,是否強(qiáng)迫inline展開并不重要。因?yàn)橹饕f明shared_ptr確實(shí)對(duì)效率有影響,不是為了加快整個(gè)程序的執(zhí)行效率。
            明天我去公司再試試看把map換成vector,不過估計(jì)結(jié)果也差不多~@skyscribe
              回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-01 08:49 | 遠(yuǎn)古毛利人
            樓主是否在Release下測(cè)試的?  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-01 08:50 | Kevin Lynx
            這個(gè)測(cè)試?yán)哟_實(shí)有問題。把一個(gè)對(duì)象(shared_ptr)和一個(gè)指針(string*)分別作為map的key,對(duì)象肯定會(huì)比指針慢。對(duì)象用于map的key會(huì)涉及到很多復(fù)制操作。  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-01 09:10 | null
            想不出來你為什么要用(string*)做key,既然用了指針,那肯定不是為了排序,如果是為了查找,那應(yīng)該用hash_map(boost::unordered_map)  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-01 09:29 | zuhd
            用指針做key有什么意義呢?它和int做索引有什么區(qū)別?  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-01 14:34 | test
            主要是你的那個(gè)字符串太小了,相比較shared_ptr的構(gòu)造計(jì)數(shù)等動(dòng)作耗時(shí)差不多。所以才會(huì)差一倍。  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-02 11:42 | luck
            指針版的沒有銷毀string的指針  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-02 17:52 | luck
            把compareP類的操作符bool operator() (const T lh,const T rh) const
            的參數(shù)改為引用傳遞就會(huì)減少很大部分的差別  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-03 13:47 | 唐新發(fā)
            下面的測(cè)試顯示shared_ptr相對(duì)于使用原始指針會(huì)慢上很多(2-4倍),主要是來自引用計(jì)數(shù)及其帶來的指針對(duì)象的創(chuàng)建銷毀時(shí)間,下面的測(cè)試代碼都是指針創(chuàng)建及傳遞操作:

            #include <windows.h>
            #include <cassert>
            #include <iostream>
            #include <string>
            #include <boost/shared_ptr.hpp>

            typedef int type;
            typedef type* raw_ptr_type;
            typedef boost::shared_ptr<type> shared_ptr_type;

            template<typename T>
            void use(T ptr)
            {
            T ptr1 = ptr;
            T ptr2 = ptr;
            T ptr3 = ptr;
            }

            int main(int argc, char* argv[])
            {
            size_t count = 500000;

            if(argc > 1)
            count = atoi(argv[1]);

            DWORD oldtime, shared_ptr_time, raw_ptr_time;

            {
            oldtime = GetTickCount();
            for (size_t i = 0; i < count ; ++i)
            {
            raw_ptr_type ptr(new type);
            use(ptr);
            delete ptr;
            }
            raw_ptr_time = GetTickCount() - oldtime;
            }

            {
            oldtime = GetTickCount();
            for (size_t i = 0; i < count ; ++i)
            {
            shared_ptr_type ptr(new type);
            use(ptr);
            }
            shared_ptr_time = GetTickCount() - oldtime;
            }

            assert(raw_ptr_time <= shared_ptr_time);

            std::cout << "count(" << count << ") raw_ptr(" << raw_ptr_time << " ms) shared_ptr(" << shared_ptr_time << " ms) diff(+" << shared_ptr_time - raw_ptr_time << " ms, +" << double(shared_ptr_time - raw_ptr_time)/double(raw_ptr_time)*100 << "%)"<< std::endl;

            return EXIT_SUCCESS;
            }

            vc2008下 Release 禁用優(yōu)化的三次執(zhí)行:
            count(500000) raw_ptr(110 ms) shared_ptr(591 ms) diff(+481 ms, +437.273%)
            count(500000) raw_ptr(110 ms) shared_ptr(471 ms) diff(+361 ms, +328.182%)
            count(500000) raw_ptr(120 ms) shared_ptr(471 ms) diff(+351 ms, +292.5%)

            vc2008下 Release  完全優(yōu)化的三次執(zhí)行:
            count(500000) raw_ptr(111 ms) shared_ptr(330 ms) diff(+219 ms, +197.297%)
            count(500000) raw_ptr(100 ms) shared_ptr(331 ms) diff(+231 ms, +231%)
            count(500000) raw_ptr(110 ms) shared_ptr(351 ms) diff(+241 ms, +219.091%)
              回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-04 16:28 | 99讀書人網(wǎng)上書城
            這個(gè)測(cè)試?yán)哟_實(shí)有問題  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2009-07-05 22:50 | 李錦俊
            堆中申請(qǐng)內(nèi)存和銷毀內(nèi)存是極為耗時(shí)的。應(yīng)該把測(cè)試做得更單純一點(diǎn)才對(duì)。  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2011-03-26 12:54 | 張立斌
            for (size_t i(0) ; i<500000 ; ++i)
            {
            boost::shared_ptr<string> pstr(new string);
            char buffer[32];
            *pstr = itoa(i,buffer,10);
            container[pstr]=0;
            //shared_ptr在此處釋放內(nèi)存
            }
            for (size_t i(0) ; i<500000 ; ++i)
            {
            string *pstr = new string;
            char buffer[32];
            *pstr = itoa(i,buffer,10);
            container[pstr]=0;
            //內(nèi)存泄露
            }
            如果不信,你可以把string改為自定義的類,并在構(gòu)造和析構(gòu)函數(shù)中加入輸出。  回復(fù)  更多評(píng)論
              
            # re: 關(guān)于boost庫中shared_ptr執(zhí)行速度的簡(jiǎn)單測(cè)試
            2012-08-30 01:43 | qindh
            我測(cè)試了share_ptr版式1847,new delete版是104,測(cè)試是用的VS2010版本,share_ptr不是用的boost庫,是用的VS2010自帶的  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2012年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(5)

            隨筆檔案

            test

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久国产精品免费一区二区三区| 思思久久好好热精品国产| 国产99久久久久久免费看| 女同久久| 久久AV高清无码| 亚洲国产成人久久综合野外| 亚洲va国产va天堂va久久| 精品久久一区二区| 久久久久久久波多野结衣高潮 | 国产精品女同一区二区久久| 少妇被又大又粗又爽毛片久久黑人| 一本久久知道综合久久| 久久噜噜久久久精品66| 久久亚洲精品成人av无码网站| 久久综合色区| 91精品国产高清久久久久久91| 一本一本久久A久久综合精品 | 久久国产精品-久久精品| 亚洲欧美成人久久综合中文网| 精品久久久久久国产| 少妇高潮惨叫久久久久久| 亚洲欧洲中文日韩久久AV乱码| 久久91精品国产91久久户| 日韩精品久久无码人妻中文字幕| 热RE99久久精品国产66热| 亚洲国产成人久久精品影视 | 性欧美大战久久久久久久| 一本大道加勒比久久综合| 国产精品国色综合久久| 无码人妻久久一区二区三区| 久久久久亚洲AV片无码下载蜜桃| 久久亚洲电影| 色99久久久久高潮综合影院| 中文字幕成人精品久久不卡| 狠狠色丁香久久综合五月| 97久久天天综合色天天综合色hd| 欧美噜噜久久久XXX| 天天躁日日躁狠狠久久| 久久狠狠高潮亚洲精品| 99久久精品国产高清一区二区 | 亚洲国产成人久久综合区|