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

            成人综合久久精品色婷婷| 99热成人精品热久久669| 精品久久久久久无码国产| 免费精品99久久国产综合精品| 久久久久亚洲国产| 久久狠狠爱亚洲综合影院| 精品人妻伦九区久久AAA片69| 欧美久久久久久午夜精品| 99久久精品免费| 日韩十八禁一区二区久久| 午夜精品久久久久久久无码| 免费精品国产日韩热久久| 77777亚洲午夜久久多喷| 国产精品久久新婚兰兰| 久久精品人人做人人爽电影| 亚洲中文字幕久久精品无码APP| 久久精品国产第一区二区三区| 久久AV高清无码| 国产精品成人精品久久久| 久久人人爽人人精品视频| 久久精品国产精品亚洲精品 | 伊人情人综合成人久久网小说| 一个色综合久久| 久久婷婷五月综合国产尤物app| 国产亚洲综合久久系列| 欧美久久久久久精选9999| 无码国内精品久久人妻| 国产香蕉97碰碰久久人人| 久久婷婷国产剧情内射白浆| 久久久久亚洲AV成人片| 亚洲综合久久综合激情久久| 色狠狠久久综合网| 青青青国产成人久久111网站| 色天使久久综合网天天| 成人亚洲欧美久久久久| 久久综合国产乱子伦精品免费| 99久久成人18免费网站| 少妇久久久久久久久久| 性做久久久久久免费观看| 久久精品午夜一区二区福利| 久久国产福利免费|