• <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,簡稱RVO),是這么一種優(yōu)化機制:當(dāng)函數(shù)需要返回一個對象的時候,如果自己創(chuàng)建一個臨時對象用戶返回,那么這個臨時對象會消耗一個構(gòu)造函數(shù)(Constructor)的調(diào)用、一個復(fù)制構(gòu)造函數(shù)的調(diào)用(Copy Constructor)以及一個析構(gòu)函數(shù)(Destructor)的調(diào)用的代價。而如果稍微做一點優(yōu)化,就可以將成本降低到一個構(gòu)造函數(shù)的代價,下面是在Visual Studio 2008的Debug模式下做的一個測試:(在GCC下測試的時候可能編譯器自己進行了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òu)造函數(shù)(Constructor)的調(diào)用、一個復(fù)制構(gòu)造函數(shù)的調(diào)用(Copy Constructor)以及一個析構(gòu)函數(shù)(Destructor)的調(diào)用的代價。

            而如果把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());
            }

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

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

            您可能對下面的文章也感興趣:
            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. 阻止編譯器自動生成copy函數(shù) (6.4)
            分類:C++編程標(biāo)簽:

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

            <2011年9月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            導(dǎo)航

            統(tǒng)計

            留言簿(1)

            文章分類

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            友情鏈接:C++博客 LoveBeyond 代碼瘋子 程序人生 C++技術(shù)博客
            久久99精品久久久久久齐齐| 999久久久无码国产精品| 亚洲一本综合久久| 久久国产综合精品五月天| 一本色道久久HEZYO无码| 国产精品久久影院| 久久久无码精品亚洲日韩京东传媒| 久久久久久久久66精品片| 97热久久免费频精品99| 久久国产精品免费一区二区三区 | 久久天天躁夜夜躁狠狠躁2022| 久久久久免费看成人影片| 久久精品国产只有精品66 | 亚洲日韩中文无码久久| 久久国产精品一区二区| 精品久久久中文字幕人妻| 亚洲另类欧美综合久久图片区| 丁香五月网久久综合| 精品久久久久久综合日本| 亚洲国产精品无码成人片久久| 亚洲另类欧美综合久久图片区| 狠狠色噜噜狠狠狠狠狠色综合久久| 一本久久a久久精品vr综合| 久久人人爽人人爽人人爽| 伊人久久大香线蕉av一区| 久久九九兔免费精品6| 国产精品久久永久免费| 久久成人精品视频| 久久国产综合精品五月天| 久久久WWW成人免费精品| 久久午夜免费视频| 久久精品人人做人人爽电影蜜月| 青青草国产精品久久| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久精品无码一区二区三区免费| 日本精品久久久中文字幕| 思思久久99热只有频精品66| 国产Av激情久久无码天堂 | 久久亚洲精品无码AV红樱桃| 久久99国产乱子伦精品免费| 欧洲国产伦久久久久久久|