Is memcpy() usually faster than strcpy() (on most real platforms)? (I assume that size of the string is known.)
memcpy()通常比strcpy()更快(在大多数真实平台上)吗? (我假设字符串的大小是已知的。)
If I remember i386 assembler correctly, there are "loop" commands 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.
如果我正确地记得i386汇编程序,则会有“循环”命令复制给定数量的字节或字。所以这是最快的方法,而strcpy()i386汇编程序实现将在普通循环中使用手动检查'\ 0'。
So I feel that on x86 memcpy() is faster than strcpy().
所以我觉得在x86上memcpy()比strcpy()更快。
What's about other architectures?
其他架构是什么?
2 个解决方案
#1
20
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()
.
如果您知道要复制的数据的大小,那么memcpy()应该比strcpy()快或快。否则,memcpy()不能单独使用,strcpy()应该比strlen()后跟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.
memcpy()和/或strcpy()和/或strlen()的许多实现都是为了有效处理大量数据而设计的。这通常意味着额外的启动开销(例如,确定对齐,设置SIMD,高速缓存管理等)并使这些实现不好(慢)用于复制少量数据(这在编写良好的代码中更可能)。因此,“应该快或快”并不一定意味着“快或快”。例如,对于少量数据,针对大量数据优化的memcpy()可能比未针对大量数据优化的strcpy()明显更慢。
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.
还要注意,这里的主要问题是通用代码(例如memcpy()和strcpy())无法针对特定情况进行优化。最好的解决方案是拥有多种功能 - 例如memcpy_small()针对复制少量数据进行了优化,memcpy_large()针对无法避免复制大量数据的错误代码进行了优化。
#2
9
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.
在几乎任何平台上,当复制相同数量的字节时,memcpy()将比strcpy()更快。只有当字符串的最大允许大小远大于其实际大小时,strcpy()或其任何“安全”等价物才会胜过memcpy()。如果一个缓冲区可以容纳一个长达一百万个字符的字符串,并且想要将字符串复制到另一个百万字节的缓冲区,那么即使第一个缓冲区中的字符串是,memcpy()也必须复制一百万个字节。只有两个字符长。然而,strcpy()函数或其等价物能够提前退出,并且复制两个字符可能比memcpy()复制一百万个所需的时间更少。
#1
20
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()
.
如果您知道要复制的数据的大小,那么memcpy()应该比strcpy()快或快。否则,memcpy()不能单独使用,strcpy()应该比strlen()后跟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.
memcpy()和/或strcpy()和/或strlen()的许多实现都是为了有效处理大量数据而设计的。这通常意味着额外的启动开销(例如,确定对齐,设置SIMD,高速缓存管理等)并使这些实现不好(慢)用于复制少量数据(这在编写良好的代码中更可能)。因此,“应该快或快”并不一定意味着“快或快”。例如,对于少量数据,针对大量数据优化的memcpy()可能比未针对大量数据优化的strcpy()明显更慢。
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.
还要注意,这里的主要问题是通用代码(例如memcpy()和strcpy())无法针对特定情况进行优化。最好的解决方案是拥有多种功能 - 例如memcpy_small()针对复制少量数据进行了优化,memcpy_large()针对无法避免复制大量数据的错误代码进行了优化。
#2
9
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.
在几乎任何平台上,当复制相同数量的字节时,memcpy()将比strcpy()更快。只有当字符串的最大允许大小远大于其实际大小时,strcpy()或其任何“安全”等价物才会胜过memcpy()。如果一个缓冲区可以容纳一个长达一百万个字符的字符串,并且想要将字符串复制到另一个百万字节的缓冲区,那么即使第一个缓冲区中的字符串是,memcpy()也必须复制一百万个字节。只有两个字符长。然而,strcpy()函数或其等价物能够提前退出,并且复制两个字符可能比memcpy()复制一百万个所需的时间更少。