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

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評(píng)論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            利用C++模板,代替虛函數(shù),實(shí)現(xiàn)類的靜態(tài)多態(tài)性(加入性能測(cè)試部分)

                熟悉模板編程的朋友或許聽到過這個(gè)技巧或者模式:Barton-Nackmann 技巧或者稱 奇異循環(huán)模板模式(Curiously Recurring Template Prattern)。
                
            其實(shí)在 c++ 編程語(yǔ)言》這本bible 書里,在模板那章提到過一個(gè)很奇妙的類的實(shí)現(xiàn),用的就是這個(gè)技術(shù)。當(dāng)時(shí),我就被C++模板技術(shù)嘆為觀止。近期在學(xué)boost庫(kù)時(shí)偶然碰到了這個(gè)技巧,同時(shí)在寫一個(gè)類時(shí)引發(fā)了我的思考,這里就利用這個(gè)技巧來實(shí)現(xiàn),靜態(tài)多態(tài)函數(shù)(我自己發(fā)明的叫法,呵呵)。
             
            我們知道C++的多態(tài)函數(shù)會(huì)帶來很多靈活性,但是不可避免的它是有運(yùn)行時(shí)的性能損失的。 c++的另一個(gè)強(qiáng)大特性就是模板了。模板給C++帶來了,編譯時(shí)的多態(tài),通過模板元編程,C++可以實(shí)現(xiàn)類似C#javarefection的特性。這里我就舉來實(shí)現(xiàn)利用模板來代替虛函數(shù)。
             
            例子1

            #include <iostream>
            using namespace std;
             
            class common_base
            {
            public:
              virtual void fun
            ()=0
            };
            class common_derive:public common_base
            {
            public:
              void fun
            ()
              { cout<<"in common_derive fun()"<<endl;
            };
             
            void main()
            {
              common_base * pb = new common_derive;
              pb->fun();
            }

              這是一個(gè)最普通的多態(tài)例子,下面看看一個(gè)比較有意思的例子:
            例子2

            template<typename T>
            class class1
            {
            public:
                class1(T t):m_val(t){}
                virtual T getVal(){
                    cout<<"in class1,val =="<< m_val <<endl;
                    return m_val;
                }
            private:
                T m_val;
            };

            class derived: public class1<int>
            {
            public:
                derived(int i):class1<int>(i){}
                int getVal()
                {
                    cout<<"in derived"<<endl;
                    return class1<int>::getVal();
                }
            };

            template<typename T>
            class derived2: public class1<T>
            {
            public:
                derived2(T val):class1<T>(val){}
                T getVal()
                {
                    cout<<"in derived2"<<endl;
                    return class1<T>::getVal();
                }
            };

            void main()
            {
                class1<int> * pbase = new derived(10);
                pbase->getVal();

                class1<int> * pb2 = new derived2<int>(10);
                pb2->getVal();
            }

            這個(gè)例子我的用意是說明:模板類的虛函數(shù)多態(tài),而且派生類可以有兩種選擇,一個(gè)實(shí)現(xiàn)為普通類,繼承的是模板基類的特化類,一個(gè)就實(shí)現(xiàn)模板類(如 derived2)。很明顯模板繼承類有著普通類不可比擬的靈活性。

            下面是這篇文章的重頭戲了,也是本文的目的所在。
            我們看到例子12都采用虛函數(shù)來實(shí)現(xiàn)多態(tài),這個(gè)是一般選擇,如何用模板來實(shí)現(xiàn)多態(tài),而不是虛函數(shù)呢?
            看這個(gè)例子:

            template<class derive>
            class base
            {
            public:
                void print()
                {
                    derive::print();
                }
                void m_print()
                {
                    downcast()->derive::m_print();
                }
            protected:
                inline derive * downcast()
                {
                    return static_cast<derive *>(this);
                };
                inline const derive * downcast()const
                {
                    return static_cast<const derive *>(this);
                };
            };

            class der:public base<der>
            {
            public:
                der(int foo):_foo(foo){}
                static void print()
                {
                    cout<<"in der print"<<endl;
                };
                void m_print()
                {
                    cout<<"in der member fun m_print"<<endl;
                    cout<<"has member foo="<<_foo<<endl;
                }
            private:
                int _foo;
            };

            template<class base>
            class der2:public base
            {
            public:
                static void print()
                {
                    cout<<"in der2 print"<<endl;
                };
                void m_print()
                {
                    cout<<"in der2 member fun m_print"<<endl;
                }
            };

            class tmpclass
            {
            public:
                void test()
                { cout<<"in test"<<endl;}
            };

            int main(int argc, char* argv[])
            {
                //
            模板實(shí)現(xiàn)虛函數(shù)多態(tài)
                base<der> * pb= new der(100);
                pb->print();
                pb->m_print();

                //
            動(dòng)態(tài)繼承
                der2<tmpclass> d2;
                d2.print();
                d2.m_print();
                d2.test();

                return 0;
            }

            哈哈,看class der是不是同樣實(shí)現(xiàn)了多態(tài)??而且語(yǔ)義和虛函數(shù)一致。可以進(jìn)一步提取downcast()部分到一個(gè)基類實(shí)現(xiàn)更普遍的寫法。

            后面我實(shí)現(xiàn)了一個(gè)動(dòng)態(tài)繼承的類der2,它同樣提供了靈活的繼承用法,可惜好像因編譯器而定,在vc6環(huán)境下是不能通過編譯的,而在g++下是ok的。

             下面我編寫了一個(gè)性能測(cè)試?yán)虂頊y(cè)試?yán)锰摂M函數(shù)實(shí)現(xiàn)多態(tài)和模板實(shí)現(xiàn)多態(tài)的性能。代碼如下

            #include <iostream>
            using namespace std;
            #include <sys/time.h>

            class common_base
            {
            public:
                common_base(int iloop){_iloop=iloop;}
                virtual void virtual_fun()=0;
                void timesum_fun()
                {
                    struct timeval begin,end;
                    gettimeofday(&begin, NULL);
                    for(int i=0;i<_iloop;i++)
                        virtual_fun();
                    gettimeofday(&end, NULL);
                    cout<< "using time :" << end.tv_sec-begin.tv_sec + (end.tv_usec - begin.tv_usec)/1000000.0<<"  second"<<endl;
                };
            private:
                int _iloop;
            };
            class common_derive:public common_base
            {
            public:
                common_derive(int iloop):common_base(iloop){_foo=0;}
                void virtual_fun()
                {
                    ++_foo;
                    --_foo;
                }
            private:
                int _foo;
            };

            template<class derive>
            class base
            {
            public:
                base(int iloop){_iloop=iloop;}
                void timesum_fun()
                {
                    struct timeval begin,end;
                    gettimeofday(&begin, NULL);
                    for(int i=0;i<_iloop;i++)
                        templ_fun();

                    gettimeofday(&end, NULL);
                    cout<< "using time :" << end.tv_sec-begin.tv_sec + (end.tv_usec - begin.tv_usec)/1000000.0<<"  second"<<endl;
                }
                inline void templ_fun()
                {
                    downcast()->derive::templ_fun();
                }
            protected:
                inline derive * downcast()
                {
                    return static_cast<derive *>(this);
                };
                inline const derive * downcast()const
                {
                    return static_cast<const derive *>(this);
                };
            private:
                int _iloop;
            };

            class der:public base<der>
            {
            public:
                der(int iloop):base<der>(iloop){_foo=0;}
                inline void templ_fun()
                {
                    ++_foo;
                    --_foo;
                }
            private:
                int _foo;
            };

            int main()
            {
              int loop=1000*1000*100;
              common_base * pb = new common_derive(loop);
              base<der> * ptempb= new der(loop);
              for(int i =3;i-->0;)
              {
                  cout<<"virtual function test: looptime="<<loop<<endl;
                  pb->timesum_fun();
                  cout<<"template function test: looptime="<<loop<<endl;
                  ptempb->timesum_fun();
              }
              delete pb;
              delete ptempb;
              return 0;
            }

            我編譯了兩個(gè)版本一個(gè)優(yōu)化版本一個(gè)未優(yōu)化版本,運(yùn)行測(cè)試結(jié)果讓我有點(diǎn)意外:

            這是未優(yōu)化版本的,結(jié)果顯示這兩種方法不相上下,虛函數(shù)還略優(yōu),~O

            ./cmp_test
            virtual function test: looptime=100000000
            using time :1.03824  second
            template function test: looptime=100000000
            using time :1.63043  second
            virtual function test: looptime=100000000
            using time :1.03768  second
            template function test: looptime=100000000
            using time :1.62773  second
            virtual function test: looptime=100000000
            using time :1.63104  second

            運(yùn)行優(yōu)化版本,性能優(yōu)勢(shì)一下體現(xiàn)出來了,模板實(shí)現(xiàn)是虛函數(shù)的十倍:

            ./cmp_test_optimize
            virtual function test: looptime=100000000
            using time :0.615542  second
            template function test: looptime=100000000
            using time :0.055584  second
            virtual function test: looptime=100000000
            using time :0.624778  second
            template function test: looptime=100000000
            using time :0.057419  second
            virtual function test: looptime=100000000
            using time :0.624977  second
            template function test: looptime=100000000
            using time :0.059442  second

            有點(diǎn)驚人是不是?這個(gè)差別就是因?yàn)樘摵瘮?shù)是不可優(yōu)化和內(nèi)聯(lián)的,而模板函數(shù)是可內(nèi)聯(lián)的,這個(gè)性能差異就很大,再次隨著虛表的增大虛函數(shù)的調(diào)用是有性能退化的,而這點(diǎn)對(duì)于模板函數(shù)來說是沒有的,因?yàn)樵诰幾g時(shí),這一切都是靜態(tài)了。不過客觀的說,虛函數(shù)多態(tài)是C++語(yǔ)言內(nèi)置的,在復(fù)雜度方面,應(yīng)該首選虛函數(shù),這里提供的這個(gè)方法只是作者在學(xué)習(xí)過程中的一個(gè)體會(huì),模板的世界實(shí)在是太奇妙和高深了。

            posted on 2008-05-22 22:41 肥仔 閱讀(7376) 評(píng)論(5)  編輯 收藏 引用 所屬分類: C++ 基礎(chǔ)

            評(píng)論

            # re: 利用C++模板,代替虛函數(shù),實(shí)現(xiàn)類的靜態(tài)多態(tài)性(加入性能測(cè)試部分)  回復(fù)  更多評(píng)論   

            “因?yàn)樵诰幾g時(shí),這一切都是靜態(tài)了;在復(fù)雜度方面,應(yīng)該首選虛函數(shù);”

            虛函數(shù)的動(dòng)態(tài)特性的主要威力是大型項(xiàng)目中,模塊兒之間的可插拔
            2010-08-28 12:04 | 路人

            # re: 利用C++模板,代替虛函數(shù),實(shí)現(xiàn)類的靜態(tài)多態(tài)性(加入性能測(cè)試部分)  回復(fù)  更多評(píng)論   

            哥運(yùn)行的未優(yōu)化版本可以看出,虛函數(shù)的優(yōu)勢(shì)還是很明顯的。。。\

            virtual function test: looptime=100000000
            using time :0.849799 second
            template function test: looptime=100000000
            using time :1.6305 second
            virtual function test: looptime=100000000
            using time :0.848855 second
            template function test: looptime=100000000
            using time :1.6228 second
            virtual function test: looptime=100000000
            using time :0.838681 second
            template function test: looptime=100000000
            using time :1.62295 second
            2010-10-15 19:26 | sipo

            # re: 利用C++模板,代替虛函數(shù),實(shí)現(xiàn)類的靜態(tài)多態(tài)性(加入性能測(cè)試部分)  回復(fù)  更多評(píng)論   

            請(qǐng)問一下怎么優(yōu)化?
            2013-11-25 16:30 | Mr.Raindrop

            # re: 利用C++模板,代替虛函數(shù),實(shí)現(xiàn)類的靜態(tài)多態(tài)性(加入性能測(cè)試部分)  回復(fù)  更多評(píng)論   

            然并卵。
            common_base * pb = new common_derive(loop);
            // 虛函數(shù)的方式,pb的指針類型是一樣的,都是基類的指針。
            base<der> * ptempb= new der(loop);
            // 模板偽多態(tài),這個(gè)ptempb指針類型都是不一樣的,不能用同一個(gè)指針指向不同子類的實(shí)例,也就失去了多態(tài)的意義。
            2015-08-21 11:32 | ngugc

            # re: 利用C++模板,代替虛函數(shù),實(shí)現(xiàn)類的靜態(tài)多態(tài)性(加入性能測(cè)試部分)[未登錄]  回復(fù)  更多評(píng)論   

            求問怎么優(yōu)化的
            2016-06-06 21:46 | hunter
            精品国产一区二区三区久久久狼| 国产精品久久久久久五月尺| 99久久er这里只有精品18| 无码AV波多野结衣久久| 久久精品国产第一区二区三区| 久久九九青青国产精品| 久久国产成人午夜aⅴ影院| 一本久久a久久精品亚洲| 狠狠狠色丁香婷婷综合久久五月| 久久艹国产| 久久男人Av资源网站无码软件| 国产视频久久| 久久99国产精品久久| 久久久久免费精品国产| 色综合久久久久| 99久久无码一区人妻a黑| 久久人人爽人人爽人人片AV不| 99久久99久久精品免费看蜜桃| 伊人久久国产免费观看视频| 久久久久夜夜夜精品国产| 婷婷久久五月天| 久久成人国产精品一区二区| 九九精品99久久久香蕉| 婷婷久久香蕉五月综合加勒比| 精品国产青草久久久久福利 | 久久婷婷国产剧情内射白浆| 日本免费久久久久久久网站 | 91久久福利国产成人精品| 三上悠亚久久精品| 久久婷婷人人澡人人爽人人爱 | 狠狠色综合网站久久久久久久| 久久综合噜噜激激的五月天| 久久精品综合网| 欧美日韩久久中文字幕| 人妻无码精品久久亚瑟影视| 亚洲伊人久久综合影院| 久久天天婷婷五月俺也去| 久久青青色综合| 亚洲αv久久久噜噜噜噜噜| 热re99久久6国产精品免费| 69国产成人综合久久精品|