• <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)化機制:當函數(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++編程標簽:

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

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

            導(dǎo)航

            統(tǒng)計

            留言簿(1)

            文章分類

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            友情鏈接:C++博客 LoveBeyond 代碼瘋子 程序人生 C++技術(shù)博客
            久久中文字幕人妻熟av女| 精品少妇人妻av无码久久| 麻豆久久久9性大片| 久久亚洲国产精品成人AV秋霞| 欧美日韩久久中文字幕| 狠狠色婷婷久久一区二区三区| 久久www免费人成看国产片| 久久精品国产色蜜蜜麻豆| 精品久久香蕉国产线看观看亚洲 | 久久成人国产精品免费软件| 久久亚洲AV成人出白浆无码国产| 亚洲综合婷婷久久| 无码超乳爆乳中文字幕久久| 99久久免费只有精品国产| 777午夜精品久久av蜜臀| www亚洲欲色成人久久精品| 97久久国产露脸精品国产| 国产精品成人无码久久久久久 | 国产一久久香蕉国产线看观看| 久久久中文字幕日本| 精品久久久久久| 久久久av波多野一区二区| 亚洲国产成人精品无码久久久久久综合| 久久久精品人妻一区二区三区四 | 国产精品狼人久久久久影院| 亚洲精品美女久久久久99| 久久强奷乱码老熟女| 97精品伊人久久久大香线蕉| 7777久久亚洲中文字幕| 国内精品九九久久久精品| 欧美一区二区三区久久综合| 久久精品青青草原伊人| 免费精品久久天干天干| 99久久国产亚洲综合精品| 亚洲欧美成人久久综合中文网 | 狠狠色丁香婷综合久久| 久久久精品人妻一区二区三区蜜桃| 99精品久久久久久久婷婷| 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 久久99国产综合精品免费| 男女久久久国产一区二区三区|