简体   繁体   中英

Does accessing fields in a struct take more time?

Does accessing fields deep in a struct take additional time (assembly instructions) or are those memory locations hardcoded by the compiler? I'd like to logically keep certain things together in a struct but want to understand if I may be taking a performance hit because of it.

For example, in the following code, is the write to scd any slower than a write to d2 ?

typedef struct a_struct_tag {
  uint8_t d;
} a_struct_t;

typedef struct my_struct_tag {
  uint8_t    a;
  uint8_t    b[8];
  a_struct_t c;
} my_struct_t;

int main()
{
 my_struct_t s;
 s.c.d = 3;

 a_struct_t d2;
 d2.d = 4;
}

There is no difference. Let's take a look at a bit of assembly output from objdump -S binary :

int main()
{
  40053c:       55                      push   %rbp
  40053d:       48 89 e5                mov    %rsp,%rbp
  400540:       48 83 ec 30             sub    $0x30,%rsp
  400544:       64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
  40054b:       00 00 
  40054d:       48 89 45 f8             mov    %rax,-0x8(%rbp)
  400551:       31 c0                   xor    %eax,%eax
my_struct_t s;
s.c.d = 3;
  400553:       c6 45 e9 03             movb   $0x3,-0x17(%rbp)

a_struct_t d2;
d2.d = 4;
  400557:       c6 45 df 04             movb   $0x4,-0x21(%rbp)
}
 .... //the rest is not interesting, or at least I believe so

Now I am not an expert in x86 assembly, but we can clearly see, that there is just one instruction for both commands.

And why is that so? If you have a struct, that is clearly defined in terms of it's size, then during the compilation process every offset can be calculated. Now it just "put a value X in the memory cell Y".

The level of nesting is not important.

As for the general guidelines towards the performance tuning, let me cite Donald Knuth:

Premature optimization is the root of all evil.

Better tune your algorithms and IO than worry about the compiler's work. Compiler can do miracles (literally!) in terms of optimisation, if you allow it with certain compilation flags.

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