C++
  Home arrow C++ arrow Page 3 - Temporary Variables: Chasing Temporaries A...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Temporary Variables: Chasing Temporaries Away
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2005-10-10

    Table of Contents:
  • Temporary Variables: Chasing Temporaries Away
  • Return Value Optimization
  • Vector3 test
  • Details, details… what’s that… small print?
  • Help the compiler help you

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Temporary Variables: Chasing Temporaries Away - Vector3 test


    (Page 3 of 5 )

    Lest create two vectors to work with:

    Vector3 vectB(“vectB”, 0.f, 0.f, 2.f);
    Vector3 vectC(“vectC”, 3.f, 0.f, 5.f);

    Which provide us with the expected output:

    vectB – Constructed
    vectC - Constructed

    First we shall verify that the operator-= is as efficient as we’d expect it to be:

    (void)printf(“\nTry -= operator to test reference efficiency.\n”);
    Vector3 result1(vectB);
    result1 -= vectC;
    result1.print();

    From the output this generates, we can deduce that we only need a constructor to construct ‘result1’ based on ‘vectB’ after which we only need to perform the subtraction to get our result. Apart from having to break a single statement into two separate ones, this looks like the perfect solution.

    Try -= operator to test reference efficiency.
    ‘copy of vectB’ – Copy Constructed.
    ‘copy of vectB’ -= vectC finished.
    ‘copy of vectB’ at (-3.0 0.0 –3.0)

    Next we can test the operator+. Notice that the difference between the operator+ and the operator- lies purely in the fact that the result is a local variable in the former and a constructor argument in the latter.

    (void)printf(“\nTry + operator to visualize temporary object.\n”);
    Vector3 result2(vectB + vectC);
    result2.print();

    Still, this small difference has quite an impact on the code generated by the compiler. Luckily the compiler didn’t destroy the local result when it left the function's scope, otherwise we would have needed a temporary variable there. That would have brought an additional constructor and destructor call with it!

    Try + operator to visualize temporary object.
    local result – Constructed.
    vectB + vectC finished.
    ‘copy of local result’ – Copy Constructed
    local result - Destructed.
    ‘copy of local result’ at (3.0 0.0 7.0)

    And just by returning the result by value through a constructor argument, we (or better yet the compiler) discover we are as efficient with the operator- as we are with the operator-= …

    (void)printf(“\nTry – operator for return value optimization.\n”);
    Vector3 result3(vectB – vectC);
    result3.print();

    Again we only need a constructor and the subtraction operations to come to our result. The difference here is that the subtraction is actually performed and finished before the construction of the final result –- which happens to have become the constructor argument itself. Very ingenious and efficient as well!

    Try – operator for return value optimization.
    VectB – vectC finished.
    ctor arg – Constructed.
    ctor arg at (-3.0 0.0 –3.0)

    Nowadays compilers are becoming more complex, and it might be possible for them to discover that it is safe to apply ‘Return Value Optimization’ to your functions. It doesn’t hurt to give them a helping hand though; you’ve now seen the difference it can make.

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT