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

            999久久久无码国产精品| 久久se精品一区精品二区国产| 欧美日韩精品久久久免费观看 | 伊人久久精品无码二区麻豆| 国内精品人妻无码久久久影院导航 | 欧洲精品久久久av无码电影| 99久久99久久精品国产片果冻 | 亚洲综合久久久| 久久精品国产男包| 久久综合狠狠综合久久综合88 | 日本加勒比久久精品| 久久久久久久久66精品片| 亚洲国产精品无码久久一线| 久久国产精品-国产精品| 久久受www免费人成_看片中文| 国产精品久久自在自线观看| 久久人人青草97香蕉| 久久精品成人免费观看97| 久久AV高清无码| 久久精品国产亚洲αv忘忧草| 99久久亚洲综合精品成人| 久久亚洲欧美国产精品| 久久精品一本到99热免费| 国产一区二区三精品久久久无广告| 亚洲AV成人无码久久精品老人| 久久亚洲精品无码观看不卡| 亚洲国产成人久久综合碰碰动漫3d | 久久美女人爽女人爽| 亚洲精品无码久久久久| 久久精品国产99久久久古代| 久久精品综合一区二区三区| 久久久久夜夜夜精品国产| 国产成人精品白浆久久69| 潮喷大喷水系列无码久久精品| 久久精品中文字幕一区| 亚洲中文字幕无码一久久区| 久久无码AV一区二区三区| 久久婷婷色香五月综合激情| 久久午夜夜伦鲁鲁片免费无码影视| 久久人人爽人人爽人人片av麻烦| 中文字幕精品久久|