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

            国产A三级久久精品| 伊人久久久AV老熟妇色| 久久精品人人做人人爽电影| 亚洲精品乱码久久久久久蜜桃图片| 国内精品久久久久久久久电影网| 新狼窝色AV性久久久久久| 精品久久香蕉国产线看观看亚洲 | 久久婷婷五月综合国产尤物app | 久久笫一福利免费导航 | 久久久久波多野结衣高潮| 日韩av无码久久精品免费| 91久久精品视频| 久久伊人五月丁香狠狠色| 久久香蕉综合色一综合色88| 中文字幕久久精品| 国产精品久久久久aaaa| 久久天天躁狠狠躁夜夜avapp| 久久久久亚洲AV无码永不| 久久中文精品无码中文字幕| 国内精品人妻无码久久久影院| 久久99热这里只有精品国产| 日韩av无码久久精品免费| 久久天天躁狠狠躁夜夜2020老熟妇 | 久久乐国产精品亚洲综合| 91精品国产91久久久久福利| 怡红院日本一道日本久久| 99久久精品国产一区二区| 久久久久久无码国产精品中文字幕| 久久久噜噜噜久久中文福利| 久久精品极品盛宴观看| 久久精品亚洲欧美日韩久久| 99久久精品午夜一区二区| 蜜臀av性久久久久蜜臀aⅴ麻豆| 亚洲国产精品无码久久青草| 国产国产成人久久精品| 大伊人青草狠狠久久| 国产精品久久亚洲不卡动漫| 91精品国产综合久久婷婷| 久久精品国产亚洲77777| 久久九九精品99国产精品| 亚洲中文久久精品无码|