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

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            利用C++模板,代替虛函數,實現類的靜態多態性(加入性能測試部分)

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

              這是一個最普通的多態例子,下面看看一個比較有意思的例子:
            例子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();
            }

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

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

            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[])
            {
                //
            模板實現虛函數多態
                base<der> * pb= new der(100);
                pb->print();
                pb->m_print();

                //
            動態繼承
                der2<tmpclass> d2;
                d2.print();
                d2.m_print();
                d2.test();

                return 0;
            }

            哈哈,看class der是不是同樣實現了多態??而且語義和虛函數一致。可以進一步提取downcast()部分到一個基類實現更普遍的寫法。

            后面我實現了一個動態繼承的類der2,它同樣提供了靈活的繼承用法,可惜好像因編譯器而定,在vc6環境下是不能通過編譯的,而在g++下是ok的。

             下面我編寫了一個性能測試例程來測試利用虛擬函數實現多態和模板實現多態的性能。代碼如下

            #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;
            }

            我編譯了兩個版本一個優化版本一個未優化版本,運行測試結果讓我有點意外:

            這是未優化版本的,結果顯示這兩種方法不相上下,虛函數還略優,~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

            運行優化版本,性能優勢一下體現出來了,模板實現是虛函數的十倍:

            ./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

            有點驚人是不是?這個差別就是因為虛函數是不可優化和內聯的,而模板函數是可內聯的,這個性能差異就很大,再次隨著虛表的增大虛函數的調用是有性能退化的,而這點對于模板函數來說是沒有的,因為在編譯時,這一切都是靜態了。不過客觀的說,虛函數多態是C++語言內置的,在復雜度方面,應該首選虛函數,這里提供的這個方法只是作者在學習過程中的一個體會,模板的世界實在是太奇妙和高深了。

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

            評論

            # re: 利用C++模板,代替虛函數,實現類的靜態多態性(加入性能測試部分)  回復  更多評論   

            “因為在編譯時,這一切都是靜態了;在復雜度方面,應該首選虛函數;”

            虛函數的動態特性的主要威力是大型項目中,模塊兒之間的可插拔
            2010-08-28 12:04 | 路人

            # re: 利用C++模板,代替虛函數,實現類的靜態多態性(加入性能測試部分)  回復  更多評論   

            哥運行的未優化版本可以看出,虛函數的優勢還是很明顯的。。。\

            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++模板,代替虛函數,實現類的靜態多態性(加入性能測試部分)  回復  更多評論   

            請問一下怎么優化?
            2013-11-25 16:30 | Mr.Raindrop

            # re: 利用C++模板,代替虛函數,實現類的靜態多態性(加入性能測試部分)  回復  更多評論   

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

            # re: 利用C++模板,代替虛函數,實現類的靜態多態性(加入性能測試部分)[未登錄]  回復  更多評論   

            求問怎么優化的
            2016-06-06 21:46 | hunter
            狠狠色婷婷久久一区二区| A级毛片无码久久精品免费| 久久这里只有精品视频99| 久久亚洲欧洲国产综合| 日本免费久久久久久久网站| 99久久人人爽亚洲精品美女| 久久综合给合综合久久| 国产综合成人久久大片91| 香蕉久久久久久狠狠色| 久久久久亚洲AV无码网站| 亚洲综合精品香蕉久久网97 | 久久综合日本熟妇| 亚洲午夜久久久精品影院| 久久精品国产WWW456C0M| 亚洲中文精品久久久久久不卡 | 久久人人爽人爽人人爽av| 久久久免费观成人影院 | 久久人人爽人人爽人人片AV麻烦| 久久狠狠爱亚洲综合影院| 国内精品人妻无码久久久影院导航| 色狠狠久久综合网| 久久久亚洲欧洲日产国码aⅴ| 久久91精品国产91久久小草| 99久久国产综合精品女同图片| 久久综合狠狠综合久久综合88| 日韩中文久久| 97久久精品人人澡人人爽 | 无码精品久久一区二区三区| 久久综合久久综合亚洲| 久久精品欧美日韩精品| 久久久99精品一区二区| 亚洲午夜久久久久久噜噜噜| 久久狠狠色狠狠色综合| 亚洲国产日韩欧美久久| 精品熟女少妇aⅴ免费久久| 亚洲国产精品无码久久青草| 久久天天躁狠狠躁夜夜网站 | 久久99精品久久只有精品| 亚洲精品美女久久久久99| 精品国产乱码久久久久久人妻| 韩国无遮挡三级久久|