Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memcpy() usually faster than strcpy()?

Tags:

intel

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?

like image 309
porton Avatar asked Jul 26 '14 01:07

porton


People also ask

Is memcpy faster than strcpy?

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 .

Why is memcpy so slow?

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.

What is the difference between memcpy () and strcpy () functions in C?

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.

What can I use instead of memcpy?

memmove() is similar to memcpy() as it also copies data from a source to destination.


2 Answers

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.

like image 155
Brendan Avatar answered Oct 04 '22 20:10

Brendan


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.

like image 27
supercat Avatar answered Oct 04 '22 20:10

supercat