简体   繁体   English

联合/结构数据类型和位字段引用速度

[英]Union/Structure datatype and bit field referencing speed

Using the following union typedef, is there a difference in speed between accessing MyPacket.Byte.LB compared with MyPacket.Field.LB? 使用以下并集typedef,与MyPacket.Field.LB相比,访问MyPacket.Byte.LB的速度是否有所不同?

typedef union       // create a union to assemble bytes into ints
{
int Packet;     //  contains an int
struct
    {
    char HB;
    char LB;    
    }Byte;
struct
    {
    unsigned    field1:4;
    unsigned    field2:2;
    unsigned    field3:1;
    unsigned    field4:1;
    unsigned    LB:8;
    }Field;
} packetunion;

packetunion MyPacket; packetunion MyPacket;

The only way to know for sure is to code up both versions and profile them on the target system, for multiple compiler optimization settings. 唯一可以肯定的方法是为多个编译器优化设置编写两个版本的代码,并在目标系统上对其进行概要分析。 I'd be genuinely surprised if you saw a measurable difference. 如果您看到明显的不同,我会感到非常惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM