为什么 memcpy() 和 memmove() 比指针增量快?
我正在将 N 个字节从 pSrc
复制到 pDest
.这可以在一个循环中完成:
I am copying N bytes from pSrc
to pDest
. This can be done in a single loop:
for (int i = 0; i < N; i++)
*pDest++ = *pSrc++
为什么这比 memcpy
或 memmove
慢?他们使用什么技巧来加快速度?
Why is this slower than memcpy
or memmove
? What tricks do they use to speed it up?
推荐答案
因为 memcpy 使用字指针而不是字节指针,所以 memcpy 的实现也经常用 SIMD 指令,可以一次对 128 位进行混洗.
Because memcpy uses word pointers instead of byte pointers, also the memcpy implementations are often written with SIMD instructions which makes it possible to shuffle 128 bits at a time.
SIMD 指令是汇编指令,可以对最多 16 字节长的向量中的每个元素执行相同的操作.这包括加载和存储指令.
SIMD instructions are assembly instructions that can perform the same operation on each element in a vector up to 16 bytes long. That includes load and store instructions.
相关文章