Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dirk Gregorius

memcpy vs. std::copy

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What are the main differences between theses two functions, but that std::copy seems to be more type-safe because of the use of templates rather then casting to void*? Is there a performance difference between these two functions? What about memset and std::fill as well? Is there a good reference which compares the C-Standard Library with the C++ Standard Library? -Dirk

Share this post


Link to post
Share on other sites
Advertisement
If you don;t care about the speed down to the level of assembly, std::copy and all other related functions are better for classes and OO programming. As memcpy and all other related functions are better for raw memory management as thet are translated into direct assembly codes.

Share this post


Link to post
Share on other sites
The std-methods are better for readability, but worse for speed.
The memcpy() does a very fast block-copy (In reality a block-move, which sucks, since you should be able to choose).

Share this post


Link to post
Share on other sites
quote:
Original post by tok_junior
The memcpy() does a very fast block-copy (In reality a block-move, which sucks, since you should be able to choose).

memcpy() is something like

mov edi, src
mov esi, dest
mov ecx, size
rep movsb

which in reality copies the data. My example is, of course, VERY simplified and for example the one found in some AMD code samples is much more complex. But it does basically the same thing.


--
"???" --nonpop, every single day

Share this post


Link to post
Share on other sites
So in very time critical code I should use memcpy? What about optimized versions for processors supporting SIMD instructions? Do I have to write this code myself or are there libraries fro INTEL or AMD.

Would this make sense:

// I don''t have the function ptr syntax in head
// Hopy you know what I mean
typedef void (cpy)(void* dst, const void* src, int size);
cpy* copy;

void CpySIMD(void* dst, const void* src, int size)
{
// Implementation
}

void InitSystem(void)
{
// Set the copy function
if (ProcessorSupportsSIMD)
copy = cpySIMD;

else
copy = memcopy;
}

Would something like this make sense for example in a MathLib to copy huge matrices and vectors?

-Dirk

Share this post


Link to post
Share on other sites
I thought that std::copy() specialized into memcpy() for pointer types. Or maybe that was a suggestion from someone.

Share this post


Link to post
Share on other sites
std::copy always does the right thing, memcpy doesn''t. If you want to have fun debugging strange bugs use memcpy everywhere...

You can always look up C++-library-functions in headers you include. std::copy allows overlapping regions and calls the right operator= for the type you are copying.

Optimizing performance has 3 steps: 1. profile, 2. use a better algorithm, 3. tweak implementation

Share this post


Link to post
Share on other sites
I recommend that you write the code first, then optimize later when it actually works. You might want to encapsulate functionality which you might think will be a trouble spot so you can easily change it later, but if you try to optimize as you design, it will take forever.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!