使用哪一个 - memmove()或memcpy() - 当缓冲区不重叠时?

时间:2022-04-26 20:12:23

Using memcpy() when source and destination overlap can lead to undefined behaviour - in those cases only memmove() can be used.

当源和目标重叠时使用memcpy()可能导致未定义的行为 - 在这些情况下,只能使用memmove()。

But what if I know for sure buffers don't overlap - is there a reason to use specifically memcpy() or specifically memmove()? Which should I use and why?

但是如果我确定缓冲区不重叠怎么办?是否有理由使用特定的memcpy()或特定的memmove()?我应该使用哪个以及为什么?

4 个解决方案

#1


32  

Assuming a sane library implementor, memcpy will always be at least as fast as memmove. However, on most platforms the difference will be minimal, and on many platforms memcpy is just an alias for memmove to support legacy code that (incorrectly) calls memcpy on overlapping buffers.

假设一个理智的库实现者,memcpy将始终至少与memmove一样快。但是,在大多数平台上,差异将是最小的,并且在许多平台上,memcpy只是memmove的别名,以支持遗留代码(错误地)在重叠缓冲区上调用memcpy。

Both memcpy and memmove should be written to take advantage of the fastest loads and stores available on the platform.

应该编写memcpy和memmove以利用平台上可用的最快加载和存储。

To answer your question: you should use the one that is semantically correct. If you can guarantee that the buffers do not overlap, you should use memcpy. If you cannot guarantee that the buffers don't overlap, you should use memmove.

要回答您的问题:您应该使用语义正确的问题。如果可以保证缓冲区不重叠,则应使用memcpy。如果您不能保证缓冲区不重叠,则应使用memmove。

#2


29  

memcpy() doesn't have any special handling for overlapping buffers so it lacks some checks therefore it is faster than memmove().

memcpy()对重叠缓冲区没有任何特殊处理,因此它缺少一些检查,因此它比memmove()更快。

Also on some architectures memcpy() can benefit from using CPU instructions for moving blocks of memory - something that memmove() cannot use.

另外在某些体系结构上,memcpy()可以从使用CPU指令移动内存块中受益 - 这是memmove()无法使用的内容。

#3


5  

If you're interested in which will perform better, you need to test it on the target platform. Nothing in the standard mandates how the functions are implemented and, while it may seem logical that a non-checking memcpy would be faster, this is by no means a certainty.

如果您对哪个性能更好感兴趣,则需要在目标平台上进行测试。标准中没有任何内容要求如何实现这些功能,虽然非检查memcpy的速度似乎合乎逻辑,但这绝不是确定的。

It's quite possible, though unlikely, that the person who wrote memmove for your particular compiler was a certified genius while the poor soul who got the job of writing memcpy was the village idiot :-)

为你的特定编译器编写memmove的人很有可能,虽然不太可能是一个认证的天才,而得到写memcpy工作的穷人是村里的白痴:-)

Although, in reality, I find it hard to imagine the memmove could be faster than memcpy, I don't discount the possibility. Measure, don't guess.

虽然,实际上,我发现很难想象memmove可能比memcpy更快,但我并不打算这种可能性。测量,不要猜。

#4


2  

On some ARM platform im working on, memmove was 3 times faster than memcpy for short unalligned load. As memcpy and memmove are the only truly portable type-punning mechanism, you would have thought that the would be some check by the compiler before trying to use the NEON to do it.

在我工作的某个ARM平台上,memmove比memcpy快3倍,用于短的未签名负载。由于memcpy和memmove是唯一真正的便携式打字机制,你可能会认为编译器在尝试使用NEON之前会进行一些检查。

#1


32  

Assuming a sane library implementor, memcpy will always be at least as fast as memmove. However, on most platforms the difference will be minimal, and on many platforms memcpy is just an alias for memmove to support legacy code that (incorrectly) calls memcpy on overlapping buffers.

假设一个理智的库实现者,memcpy将始终至少与memmove一样快。但是,在大多数平台上,差异将是最小的,并且在许多平台上,memcpy只是memmove的别名,以支持遗留代码(错误地)在重叠缓冲区上调用memcpy。

Both memcpy and memmove should be written to take advantage of the fastest loads and stores available on the platform.

应该编写memcpy和memmove以利用平台上可用的最快加载和存储。

To answer your question: you should use the one that is semantically correct. If you can guarantee that the buffers do not overlap, you should use memcpy. If you cannot guarantee that the buffers don't overlap, you should use memmove.

要回答您的问题:您应该使用语义正确的问题。如果可以保证缓冲区不重叠,则应使用memcpy。如果您不能保证缓冲区不重叠,则应使用memmove。

#2


29  

memcpy() doesn't have any special handling for overlapping buffers so it lacks some checks therefore it is faster than memmove().

memcpy()对重叠缓冲区没有任何特殊处理,因此它缺少一些检查,因此它比memmove()更快。

Also on some architectures memcpy() can benefit from using CPU instructions for moving blocks of memory - something that memmove() cannot use.

另外在某些体系结构上,memcpy()可以从使用CPU指令移动内存块中受益 - 这是memmove()无法使用的内容。

#3


5  

If you're interested in which will perform better, you need to test it on the target platform. Nothing in the standard mandates how the functions are implemented and, while it may seem logical that a non-checking memcpy would be faster, this is by no means a certainty.

如果您对哪个性能更好感兴趣,则需要在目标平台上进行测试。标准中没有任何内容要求如何实现这些功能,虽然非检查memcpy的速度似乎合乎逻辑,但这绝不是确定的。

It's quite possible, though unlikely, that the person who wrote memmove for your particular compiler was a certified genius while the poor soul who got the job of writing memcpy was the village idiot :-)

为你的特定编译器编写memmove的人很有可能,虽然不太可能是一个认证的天才,而得到写memcpy工作的穷人是村里的白痴:-)

Although, in reality, I find it hard to imagine the memmove could be faster than memcpy, I don't discount the possibility. Measure, don't guess.

虽然,实际上,我发现很难想象memmove可能比memcpy更快,但我并不打算这种可能性。测量,不要猜。

#4


2  

On some ARM platform im working on, memmove was 3 times faster than memcpy for short unalligned load. As memcpy and memmove are the only truly portable type-punning mechanism, you would have thought that the would be some check by the compiler before trying to use the NEON to do it.

在我工作的某个ARM平台上,memmove比memcpy快3倍,用于短的未签名负载。由于memcpy和memmove是唯一真正的便携式打字机制,你可能会认为编译器在尝试使用NEON之前会进行一些检查。