• <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++中的返回值優化

            原文出自程序人生 >> C++中的返回值優化(return value optimization)
            返回值優化(Return Value Optimization,簡稱RVO),是這么一種優化機制:當函數需要返回一個對象的時候,如果自己創建一個臨時對象用戶返回,那么這個臨時對象會消耗一個構造函數(Constructor)的調用、一個復制構造函數的調用(Copy Constructor)以及一個析構函數(Destructor)的調用的代價。而如果稍微做一點優化,就可以將成本降低到一個構造函數的代價,下面是在Visual Studio 2008的Debug模式下做的一個測試:(在GCC下測試的時候可能編譯器自己進行了RVO優化,看不到兩種代碼的區別)
            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;
            }

            函數輸出截圖如下:
            Return Value Optimization
            可以看到消耗一個構造函數(Constructor)的調用、一個復制構造函數的調用(Copy Constructor)以及一個析構函數(Destructor)的調用的代價。

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

            就只會消耗一個構造函數的成本了:
            返回值優化

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

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

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

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

            導航

            統計

            留言簿(1)

            文章分類

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            友情鏈接:C++博客 LoveBeyond 代碼瘋子 程序人生 C++技術博客
            久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 欧美久久综合九色综合| 无码人妻精品一区二区三区久久| 国内精品久久久久久久coent| 久久精品国产半推半就| 久久亚洲精品中文字幕| 熟妇人妻久久中文字幕| 久久久婷婷五月亚洲97号色| 久久婷婷成人综合色综合| 久久精品国产亚洲AV无码偷窥| 日本人妻丰满熟妇久久久久久| 中文字幕乱码久久午夜| 久久亚洲私人国产精品vA | …久久精品99久久香蕉国产| 久久精品亚洲一区二区三区浴池 | 亚洲伊人久久成综合人影院 | 伊人久久综合精品无码AV专区| 久久天天躁狠狠躁夜夜躁2014| 亚洲AV成人无码久久精品老人| 亚洲国产精品无码久久| 97久久久久人妻精品专区| 国产精品99久久久久久www| 国产午夜精品久久久久九九| 久久精品国产亚洲Aⅴ香蕉| 午夜精品久久影院蜜桃| 久久精品国产亚洲AV忘忧草18| 精品永久久福利一区二区| 国产精品九九久久免费视频 | 久久精品99久久香蕉国产色戒| 久久亚洲高清观看| 伊人久久一区二区三区无码| 少妇久久久久久被弄高潮| 91秦先生久久久久久久| 人人妻久久人人澡人人爽人人精品| 国内精品久久久久久久97牛牛 | 精品久久久久久久| 欧美激情精品久久久久久久| 成人国内精品久久久久影院| 人妻无码久久精品| 久久这里只精品国产99热| 国产精品美女久久福利网站|