c++ - CPU overhead for struct? -


in c/c++, there cpu overhead acessing struct members in comparison isolated variables?

for concrete example, should first code sample below use more cpu cycles second one? make difference if class instead of struct? (in c++)

1)

struct s {     int a;     int b; };  struct s s; s.a = 10; s.b = 20;     s.a++; s.b++; 

2)

int a; int b; = 10; b = 20; a++; b++; 

"don't optimize yet." compiler figure out best case you. write makes sense first, , make faster later if need to. fun, ran following in clang 3.4 (-o3 -s):

void __attribute__((used)) structtest() {   struct s {       int a;       int b;   };    volatile struct s s;   s.a = 10;   s.b = 20;       s.a++;   s.b++; } void __attribute__((used)) nostructtest() {   volatile int a;   volatile int b;   = 10;   b = 20;       a++;   b++; }  int main() {   structtest();   nostructtest(); } 

structtest , nostructtest have identical asm output:

pushl   %ebp movl    %esp, %ebp subl    $8, %esp movl    $10, -4(%ebp) movl    $20, -8(%ebp) incl    -4(%ebp) incl    -8(%ebp) addl    $8, %esp popl    %ebp ret 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -