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

            LoveBeyond

            C++中的返回值優(yōu)化

            原文出自程序人生 >> C++中的返回值優(yōu)化(return value optimization)
            返回值優(yōu)化(Return Value Optimization,簡(jiǎn)稱RVO),是這么一種優(yōu)化機(jī)制:當(dāng)函數(shù)需要返回一個(gè)對(duì)象的時(shí)候,如果自己創(chuàng)建一個(gè)臨時(shí)對(duì)象用戶返回,那么這個(gè)臨時(shí)對(duì)象會(huì)消耗一個(gè)構(gòu)造函數(shù)(Constructor)的調(diào)用、一個(gè)復(fù)制構(gòu)造函數(shù)的調(diào)用(Copy Constructor)以及一個(gè)析構(gòu)函數(shù)(Destructor)的調(diào)用的代價(jià)。而如果稍微做一點(diǎn)優(yōu)化,就可以將成本降低到一個(gè)構(gòu)造函數(shù)的代價(jià),下面是在Visual Studio 2008的Debug模式下做的一個(gè)測(cè)試:(在GCC下測(cè)試的時(shí)候可能編譯器自己進(jìn)行了RVO優(yōu)化,看不到兩種代碼的區(qū)別)
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            
            // C++ Return Value Optimization
            // 作者:代碼瘋子
            // 博客:http://www.programlife.net/
            #include <iostream>
            using namespace std;
             
            class Rational
            {
            public:
            	Rational(int numerator = 0, int denominator = 1) : 
            	  n(numerator), d(denominator)
            	  {
            		  cout << "Constructor Called..." << endl;
            	  }
            	  ~Rational()
            	  {
            		  cout << "Destructor Called..." << endl;
            	  }
            	  Rational(const Rational& rhs)
            	  {
            		  this->d = rhs.d;
            		  this->n = rhs.n;
            		  cout << "Copy Constructor Called..." << endl;
            	  }
            	  int numerator() const { return n; }
            	  int denominator() const { return d; }
            private:
            	int n, d;
            };
             
            //const Rational operator*(const Rational& lhs,
            //						 const Rational& rhs)
            //{
            //	return Rational(lhs.numerator() * rhs.numerator(),
            //					lhs.denominator() * rhs.denominator());
            //}
             
            const Rational operator*(const Rational& lhs,
            						 const Rational& rhs)
            {
            	cout << "----------- Enter operator* -----------" << endl;
            	Rational tmp(lhs.numerator() * rhs.numerator(),
            		lhs.denominator() * rhs.denominator());
            	cout << "----------- Leave operator* -----------" << endl;
            	return tmp;
            }
             
            int main(int argc, char **argv)
            {
            	Rational x(1, 5), y(2, 9);
            	Rational z = x * y;
            	cout << "calc result: " << z.numerator() 
            		<< "/" << z.denominator() << endl;
             
            	return 0;
            }

            函數(shù)輸出截圖如下:
            Return Value Optimization
            可以看到消耗一個(gè)構(gòu)造函數(shù)(Constructor)的調(diào)用、一個(gè)復(fù)制構(gòu)造函數(shù)的調(diào)用(Copy Constructor)以及一個(gè)析構(gòu)函數(shù)(Destructor)的調(diào)用的代價(jià)。

            而如果把operator*換成另一種形式:

            1
            2
            3
            4
            5
            6
            
            const Rational operator*(const Rational& lhs,
            				const Rational& rhs)
            {
            	return Rational(lhs.numerator() * rhs.numerator(),
            				lhs.denominator() * rhs.denominator());
            }

            就只會(huì)消耗一個(gè)構(gòu)造函數(shù)的成本了:
            返回值優(yōu)化

            原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明:
            本文出自程序人生 >> C++中的返回值優(yōu)化(return value optimization)
            作者:代碼瘋子

            您可能對(duì)下面的文章也感興趣:
            1. 空白基類最優(yōu)化 The Empty Base Class Optimization (EBCO) (13.2)
            2. 又是C++空類 (10)
            3. 在構(gòu)造函數(shù)拋出異常后析構(gòu)函數(shù)將不再被調(diào)用 (9.2)
            4. Rational Rose 2003下載地址 (8.6)
            5. 阻止編譯器自動(dòng)生成copy函數(shù) (6.4)
            分類:C++編程標(biāo)簽:

            posted on 2011-10-12 18:40 LoveBeyond 閱讀(3376) 評(píng)論(7)  編輯 收藏 引用

            <2011年10月>
            2526272829301
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(1)

            文章分類

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            友情鏈接:C++博客 LoveBeyond 代碼瘋子 程序人生 C++技術(shù)博客
            怡红院日本一道日本久久| 久久精品国产亚洲AV久| 久久久国产精品亚洲一区| 国产韩国精品一区二区三区久久| 午夜天堂av天堂久久久| 97精品久久天干天天天按摩| 精品一区二区久久| 四虎久久影院| 亚洲国产精品成人久久| 久久综合欧美成人| 久久精品亚洲AV久久久无码| 久久精品国产精品国产精品污| 亚洲综合久久综合激情久久| 久久精品中文字幕一区| 国产精品午夜久久| 国产精品久久久久9999| 99久久做夜夜爱天天做精品| 久久久91人妻无码精品蜜桃HD| 欧洲成人午夜精品无码区久久| 国产免费久久久久久无码| 91久久精一区二区三区大全| 亚洲精品国产自在久久| 久久久久亚洲精品无码网址| 嫩草影院久久99| 国产精品久久久久久吹潮| 亚洲AV日韩精品久久久久久久| 久久只这里是精品66| 久久久不卡国产精品一区二区 | 亚洲中文久久精品无码| 亚洲午夜久久久影院伊人| 久久综合亚洲鲁鲁五月天| 欧美日韩中文字幕久久久不卡| 久久精品国产免费| 久久精品无码免费不卡| 日本精品久久久久影院日本 | 囯产极品美女高潮无套久久久| 久久婷婷激情综合色综合俺也去| 久久免费精品视频| 亚洲午夜久久久影院| 久久久久亚洲AV无码专区网站| 久久精品国产亚洲av日韩|