Is memcpy()
usually faster than strcpy()
(on most real platforms)? (I assume that size of the string is known.)
If I remember i386 assembler correctly, there are loop
instructions which copy a given number of bytes or words. So it is the fastest way, while strcpy()
i386 assembler implementation would use manual checking for '\0'
in a plain loop.
So I feel that on x86 memcpy()
is faster than strcpy()
.
What's about other architectures?
If you know the length of a string, you can use mem functions instead of str functions. For example, memcpy is faster than strcpy because it does not have to search for the end of the string. If you are certain that the source and target do not overlap, use memcpy instead of memmove .
I also had some code that I really needed to speed up, and memcpy is slow because it has too many unnecessary checks. For example, it checks to see if the destination and source memory blocks overlap and if it should start copying from the back of the block rather than the front.
Answer: memcpy() function is used to copy a specified number of bytes from one memory to another. Whereas, strcpy() function is used to copy the contents of one string into another string. memcpy() function acts on memory rather than value.
memmove() is similar to memcpy() as it also copies data from a source to destination.
If you know the size of the data to be copied, then memcpy()
should be as fast or faster than strcpy()
. Otherwise, memcpy()
can't be used alone, and strcpy()
should be as fast or faster than strlen()
followed by memcpy()
.
However...
A lot of implementations of memcpy()
and/or strcpy()
and/or strlen()
are designed to efficiently handle large amounts of data. This often means additional startup overhead (e.g. determining alignment, setting up SIMD, cache management, etc) and makes these implementations bad (slow) for copying small amounts of data (which is far more likely in well written code). Because of this, "should be as fast or faster" does not necessarily imply "is as fast or faster". For example, for small amounts of data an memcpy()
optimised for large amounts of data may be significantly slower than a strcpy()
that wasn't optimised for large amounts of data.
Also note that the main problem here is that generic code (e.g. memcpy()
and strcpy()
) can't be optimised for a specific case. The best solution would have been to have multiple functions - e.g. memcpy_small()
that's optimised for copying small amounts of data and memcpy_large()
that's optimised for bad code that failed avoid copying a large amount of data.
On almost any platform, memcpy()
is going to be faster than strcpy()
when copying the same number of bytes. The only time strcpy()
or any of its "safe" equivalents would outperform memcpy()
would be when the maximum allowable size of a string would be much greater than its actual size. If one has a buffer which could hold a string of up to a million characters, and one wants to copy the string to another million-byte buffer, memcpy()
would have to copy a million bytes even if the string in the first buffer was only two characters long. The strcpy()
function or its equivalents, however, would be able to early-exit, and would probably take less time to copy two characters than memcpy()
would require to copy a million.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With