0

Is the order of data members only class (so many) counts and quite influential in access efficiency ?

if yes, how is their logical reasoning
thanks much before

  • I think only the first member will be slightly faster to access than the others, because no offset is needed relative to the object's beginning – Alexey S. Larionov Oct 03 '22 at 15:53
  • 1
    Member order can influence padding and alignment https://stackoverflow.com/questions/5397447/struct-padding-in-c and https://stackoverflow.com/questions/4306186/structure-padding-and-packing – Cory Kramer Oct 03 '22 at 15:54
  • 1
    it's not worth thinking about it. – Raildex Oct 03 '22 at 15:54
  • Unless you have explicit "efficiency" requirements, and you have measured that in an optimized build this really is one of the top-two bottlenecks, then it's really not something you need to bother with. – Some programmer dude Oct 03 '22 at 16:26
  • And what is the background for your question? What is the context? Why are you asking? If you have an underlying problem then please ask directly bout it instead. – Some programmer dude Oct 03 '22 at 16:27
  • 1
    Some processors can access a member field with one instruction. The ARM series have instructions that can add an offset to an address, then load from the effective address; all in one instruction. – Thomas Matthews Oct 03 '22 at 17:10
  • 1
    A speed efficient class has it's members aligned with the processor's addressing capabilities. For example, for a processor that only fetches 32 bits, you want your members to be aligned on 32-bit boundaries. Other alignments will increase the quantity of fetches that the processor makes. – Thomas Matthews Oct 03 '22 at 17:13
  • Regarding the alignment note by @ThomasMatthews, compilers usually pick the most efficient alignment for your members on the target processor. Again, in almost all cases you never have to do anything. – Some programmer dude Oct 03 '22 at 17:16

0 Answers0