memcpy 与 C 中的赋值

发布于 2024-07-09 08:42:57 字数 96 浏览 6 评论 0原文

在什么情况下,我应该期望 memcpys 优于现代 INTEL/AMD 硬件上的分配? 我在 32 位 Intel 平台上使用 GCC 4.2.x(但我也对 64 位感兴趣)。

Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆橘 2024-07-16 08:42:57

你永远不应该指望他们能胜过作业。 原因是,当编译器认为它会更快时(如果您使用优化标志),编译器无论如何都会使用memcpy。 如果不是,并且如果该结构足够小以适合寄存器,则可以使用直接寄存器操作,这根本不需要任何内存访问。

GCC 内部有特殊的块移动模式,可以确定何时直接更改寄存器/内存单元,或何时使用 memcpy 函数。 请注意,在分配结构时,编译器在编译时知道移动将有多大,因此它可以展开小副本(在行中移动 n 次而不是循环)。 注意-mno-memcpy

-mmemcpy
-mno-memcpy
    Force (do not force) the use of "memcpy()" for non-trivial block moves.  
    The default is -mno-memcpy, which allows GCC to inline most constant-sized copies.

谁比编译器本身更清楚何时使用memcpy

You should never expect them to outperform assignments. The reason is, the compiler will use memcpy anyway when it thinks it would be faster (if you use optimize flags). If not and if the structure is reasonable small that it fits into registers, direct register manipulation could be used which wouldn't require any memory access at all.

GCC has special block-move patterns internally that figure out when to directly change registers / memory cells, or when to use the memcpy function. Note when assigning the struct, the compiler knows at compile time how big the move is going to be, so it can unroll small copies (do a move n-times in row instead of looping) for instance. Note -mno-memcpy:

-mmemcpy
-mno-memcpy
    Force (do not force) the use of "memcpy()" for non-trivial block moves.  
    The default is -mno-memcpy, which allows GCC to inline most constant-sized copies.

Who knows it better when to use memcpy than the compiler itself?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文