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

            久久久亚洲欧洲日产国码二区| 亚洲午夜无码AV毛片久久| 久久亚洲国产成人精品性色| 久久精品国产亚洲AV高清热| 品成人欧美大片久久国产欧美| 热综合一本伊人久久精品| 国产亚洲精品久久久久秋霞| 国产一区二区三区久久精品| 亚洲精品高清一二区久久| 久久国产精品无码HDAV| 亚洲精品视频久久久| 嫩草影院久久99| 久久天天躁狠狠躁夜夜2020一| 久久精品嫩草影院| 伊人久久精品无码二区麻豆| 麻豆精品久久精品色综合| 狼狼综合久久久久综合网| 久久大香萑太香蕉av| 久久久久久国产精品美女| 国产精品久久久久国产A级| 99久久免费国产精品特黄| 国产精品成人99久久久久91gav| 久久久久人妻精品一区二区三区| 亚洲&#228;v永久无码精品天堂久久| 久久精品草草草| 久久99国产精品99久久| 久久A级毛片免费观看| 久久精品国产亚洲77777| 婷婷五月深深久久精品| 久久狠狠爱亚洲综合影院 | 99国内精品久久久久久久 | yy6080久久| 久久中文字幕精品| 要久久爱在线免费观看| 久久这里只有精品首页| 国产精品美女久久福利网站| 一本久久综合亚洲鲁鲁五月天| 午夜视频久久久久一区 | 麻豆久久久9性大片| 精品久久久久久无码不卡| AV无码久久久久不卡蜜桃|