简体   繁体   中英

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 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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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