memcpy vs. std::copy

Started by
8 comments, last by Dirk Gregorius 20 years, 1 month ago
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
Advertisement
yep, measure, measure, dunno
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.
The poorest programmer in the game industry!!
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).
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, srcmov esi, destmov ecx, sizerep 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
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
I thought that std::copy() specialized into memcpy() for pointer types. Or maybe that was a suggestion from someone.
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
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.
Thanx for the tips guys. I implemented everything without any optimisation - now I was wondering how I could improve the code.

This topic is closed to new replies.

Advertisement