本文介绍了当memcpy()比memmove()快的时候,真正重要的情况是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> memcpy()和 memmove()源和目的地重叠。当缓冲区肯定不重叠时 memcpy()因为它可能会更快。

The key difference between memcpy() and memmove() is that memmove() will work fine when source and destination overlap. When buffers surely don't overlap memcpy() is preferable since it's potentially faster.

这可能会让我感到烦恼。 它是一个微优化或有实际有效的例子,当 memcpy()更快,所以我们真的需要使用 memcpy()并不坚持 memmove()无处不在?

What bothers me is this potentially. Is it a microoptimization or are there real significant examples when memcpy() is faster so that we really need to use memcpy() and not stick to memmove() everywhere?

推荐答案

最好,调用 memcpy 而不是 memmove 将保存指针比较和条件分支。对于大拷贝,这是完全不重要的。如果你做了很多小的副本,那么可能值得测量差异;这是唯一能够判断它是否重要的​​方法。

At best, calling memcpy rather than memmove will save a pointer comparison and a conditional branch. For a large copy, this is completely insignificant. If you are doing many small copies, then it might be worth measuring the difference; that is the only way you can tell whether it's significant or not.

这绝对是一个微优化,但这并不意味着你不应该使用 memcpy ,当你可以轻松地证明它是安全的。过早的悲观是许多邪恶的根源。

It is definitely a microoptimisation, but that doesn't mean you shouldn't use memcpy when you can easily prove that it is safe. Premature pessimisation is the root of much evil.

这篇关于当memcpy()比memmove()快的时候,真正重要的情况是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!