CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2005
    Posts
    382

    efficient way to copy composite types

    I'm perusing code that is structured around - presumably a bridge design. There's performance concerns surround the store functions that store composite types which contains POD types. So consider:

    Code:
    # include <iostream>
    # include <iomanip>
    
    struct foo {
     int x ; 
     int y ;
     // more stuff
    };
    
    struct bar {
      foo f;
      void store ( const foo& in ) 
      { f = in; }
    };
    
    int main() 
    {
      foo f ;
      bar b; 
      b.store ( f ) ;
    }
    For starters, there was a discussion about using std::swap to copy the structs ( though admittedly I'm not following how) or memcpy. While I haven't had a chance to benchmark a memcpy version, I question what this will 'buy' me. Thoughts/suggestions

    Thanks in advance

  2. #2
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: efficient way to copy composite types

    I think swap will be more efficient than memcpy for smaller objects because it does not use a loop. For bigger objects, locality of reference will make memcpy execute faster.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: efficient way to copy composite types

    Quote Originally Posted by mop65715 View Post
    I'm perusing code that is structured around - presumably a bridge design. There's performance concerns surround the store functions that store composite types which contains POD types. So consider:

    Code:
    # include <iostream>
    # include <iomanip>
    
    struct foo {
     int x ; 
     int y ;
     // more stuff
    };
    
    struct bar {
      foo f;
      void store ( const foo& in ) 
      { f = in; }
    };
    
    int main() 
    {
      foo f ;
      bar b; 
      b.store ( f ) ;
    }
    For starters, there was a discussion about using std::swap to copy the structs ( though admittedly I'm not following how) or memcpy. While I haven't had a chance to benchmark a memcpy version, I question what this will 'buy' me. Thoughts/suggestions

    Thanks in advance
    What you have here are PODs (Plain old data), and your compiler should be quite efficient at seeing that. You can trust your compiler to use the BEST copy method available when you write myFoo = iFoo.

    In my opinion, doing anything else but the default provided operator= can only lower your efficiency.

    To copy stuff using swap uses a compiler optimization called "Copy-and-swap" (http://en.wikibooks.org/wiki/More_C%.../Copy-and-swap). Chances are that with the release of C++0x, it will be changed to copy-and-move.

    But you need to realize that the above swap methods work you have pointer to data objects. It doesn't really work with PODs.

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: efficient way to copy composite types

    Isn't memcpy usually implemented via duffs device? If so, no loop there either!
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured