There are two distint cases.
If your struct is POD, the copy is optimized and will be as fast as memcpy (with proper optimization level).
If your struct is not POD, C++ has to call the copy constructor for your object. The copy constructor may call other functions, new operators, etc. so it will be slower than memcpy. But memcpy
will not copy the struct correcty, using memcpy
on a non-POD type results in undefined behaviour!
Note that e.g. in g++
the call to memcpy
will be inlined and optimized out. As the intention between a struct copy and a memcpy call is exactly the same (copy X bytes from location Y to Z), I do not think that the generated assembly code will differ.
Anyway, to be sure, find it out by analyzing the assembly of your code.
Edit: just read the end of the question about the function parameters. Please note that function parameter passing is usually (especially in x64) done in registers and it is much faster than memcpy
.
I've checked the assembly code and they do differ. The exact code will depend on the calling convention your current compiler uses. For me the struct is not passed in registers, rather it's passed on the stack and an actual copy is made. The three int
s are passed in %ecx
, %edx
and %r8d
. I've tried this on Windows GCC. It seems to use the Windows x64 calling convetion.
For more information on how the parameters are passed look at the specifications of your calling convention. All the details and corner cases are worked out. E.g. for x64 GCC look at System V AMD64 ABI Chapter 3.2.3 Parameter passing. For Visual Studio look here.