memcpy 与 C 中的赋值 [英] memcpy vs assignment in C

查看:30
本文介绍了memcpy 与 C 中的赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下,我应该期望 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).

推荐答案

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

You should never expect them 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 内部具有特殊的块移动模式,可以确定何时直接更改寄存器/内存单元,或何时使用 memcpy 函数.请注意,在分配结构时,编译器在编译时知道移动将有多大,因此它可以展开小副本(例如,在行中移动 n 次而不是循环).注意-mno-memcpy:

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.

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

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

这篇关于memcpy 与 C 中的赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆