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

            Zero Lee的專欄

            The Return Value Optimization[1]

            Resource from the book:
            Dov Bulka & David Mayhew <Efficient C++--- Performance programming Techiques>

            Anytime you can skip the creation and destruction of an object, you are looking at a performance gain. We will discuss an optimization often performed by compilers to speed? up your source code by transforming it and eliminating object creation. This optimization is referred to as the Return Value Optimization(RVO). Prior to delving into the RVO we need to understand how return-by-value works. We will walk through it with a simple example.

            The Mechanics of Return-by-Value
            The Complex class implements a representation for complex numbers:

            ?1 class ?Complex
            ?2
            {
            ?3 ??? // ?Complex?addition?operator

            ?4 ??? friend?Complex? operator ? + ( const ?Complex & ,? const ?Complex & );
            ?5 public
            :
            ?6 ??? //
            ?default?constructor
            ?7 ??? //
            ?Value?defaults?to?0?unless?otherwise?specified
            ?8 ??? Complex( double ?r? = ? 0.0 ,? double ?i? = ? 0.0 ):real(r),?imag(i)? {?}

            ?9
            10 ??? // ?copy?constructor

            11 ??? Complex( const ?Complex & ?c):real(c.real),?imag(c.imag)? {?}
            12
            13 ??? // ?Assignment?operator

            14 ??? Complex & ? operator ? = ( const ?Complex & ?c);
            15

            16 ??? ~ Complex()? {?}

            17 private :
            18 ??? double
            ?real;
            19 }
            ;
            The addition operator returns a Complex object by value, as in:
            1 Complex? operator ? + ( const ?Complex & ?a,? const ?Complex & ?b)
            2
            {
            3
            ????Complex?retVal;
            4 ????retVal.real? = ?a.real? +
            ?b.real;
            5 ????retVal.imag? = ?a.imag? +
            ?b.imag;
            6 ???? return
            ?retVal;
            7 }
            Suppose c1, c2, and c3 are Complex and we excute
            c3 = c1 + c2;
            How do we get the value of c1 + c2 into c3? One popular technique used by compilers is to create a temporary __result object and pass it into Complex::operator +() as a third argument. It is passed by referece. So the compiler rewrites
            1 Complex & ?Complex:: operator ? + ( const ?Complex & ?c1,? const ?Complex & ?c2)
            2
            {
            3
            ??
            4 }
            into a slightly different function:
            1 void ?Complex_Add( const ?Complex & ?__result,? const ?Complex & ?c1,? const ?Complex & ?c2)
            2
            {
            3
            ???
            4 }
            Now the original source statement
            c3 = c1 + c2;
            is transformed into(pseudocode):
            1 struct ?Complex?__tempResult;? // ?Storage.?No?constructor?here.
            2 Complex_Add(__tempResult,?c1,?c2);? // ?All?argument?passed?by?reference.
            3 c3? = ?__tempResult;? // ?Feed?result?back?into?left-hand-side.
            This return-by-value implementation opens up an optimization opportunity by eliminating the local object RetVal(inside operator +()) and computing the return value directly into the __tempResult temporary object. This is the Return Value Optimization.

            posted on 2006-11-13 19:01 Zero Lee 閱讀(263) 評論(0)  編輯 收藏 引用 所屬分類: C++ Performance

            久久艹国产| 久久婷婷五月综合国产尤物app| 久久久久久亚洲Av无码精品专口| 亚洲午夜久久久久久噜噜噜| 久久免费的精品国产V∧| 色综合久久中文综合网| 色婷婷久久久SWAG精品| 97精品伊人久久大香线蕉app| 7国产欧美日韩综合天堂中文久久久久| 色悠久久久久久久综合网| 日本久久久久亚洲中字幕| 久久99精品久久久久久不卡| 久久精品国产亚洲AV久| 亚洲午夜精品久久久久久人妖| 香蕉久久夜色精品国产2020| 青青青青久久精品国产h| 亚洲va中文字幕无码久久不卡| 久久精品国产亚洲精品| AV色综合久久天堂AV色综合在| 欧美一级久久久久久久大| 日韩精品久久久久久| 久久99久久99精品免视看动漫| 亚洲国产精品成人AV无码久久综合影院 | 久久99精品久久久久久久不卡| 久久99精品国产麻豆婷婷| 狠狠色噜噜狠狠狠狠狠色综合久久 | 国产福利电影一区二区三区,免费久久久久久久精 | 久久精品国产亚洲AV无码娇色| 色偷偷88欧美精品久久久| 99久久国产综合精品网成人影院| 无遮挡粉嫩小泬久久久久久久| 亚洲&#228;v永久无码精品天堂久久| 久久久久久亚洲AV无码专区| 久久久无码一区二区三区| 亚洲AV无码成人网站久久精品大| 国产精品久久婷婷六月丁香| 性做久久久久久久久| 久久天天躁狠狠躁夜夜躁2014| 欧美国产成人久久精品| 亚洲Av无码国产情品久久| 怡红院日本一道日本久久 |