• <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 閱讀(268) 評論(0)  編輯 收藏 引用 所屬分類: C++ Performance

            精品国产乱码久久久久久郑州公司 | 91久久精品视频| 2020最新久久久视精品爱| 久久久久亚洲精品男人的天堂| 香蕉久久夜色精品国产尤物| 久久婷婷色香五月综合激情 | 国产精品欧美久久久久无广告| 无码任你躁久久久久久| 麻豆AV一区二区三区久久 | 国产999精品久久久久久| 久久久www免费人成精品| 精品久久久无码中文字幕| 国产成人精品综合久久久| 91精品国产色综久久| 亚洲国产美女精品久久久久∴| 日韩欧美亚洲综合久久影院Ds| 好久久免费视频高清| 无码人妻久久一区二区三区免费丨 | 久久久黄片| 一级做a爰片久久毛片16| 久久精品蜜芽亚洲国产AV| 欧洲精品久久久av无码电影| 久久噜噜久久久精品66| 99久久亚洲综合精品成人| 99久久久国产精品免费无卡顿| 一本久久知道综合久久| 亚洲日本va午夜中文字幕久久 | 精品久久久久久久久久中文字幕 | 一级做a爰片久久毛片人呢| 国内精品伊人久久久久网站| 久久美女网站免费| 日产精品久久久久久久| 无码国内精品久久人妻蜜桃| 亚洲人成伊人成综合网久久久| 色综合久久天天综线观看| 久久综合精品国产一区二区三区| 国产午夜电影久久| 久久久久亚洲AV成人网| 日日狠狠久久偷偷色综合免费| 亚洲а∨天堂久久精品| 久久免费看黄a级毛片|