c++ - Coding Practices which enable the compiler/optimizer to make a faster program -


many years ago, c compilers not particularly smart. workaround k&r invented register keyword, hint compiler, maybe idea keep variable in internal register. made tertiary operator generate better code.

as time passed, compilers matured. became smart in flow analysis allowing them make better decisions values hold in registers possibly do. register keyword became unimportant.

fortran can faster c sorts of operations, due alias issues. in theory careful coding, 1 can around restriction enable optimizer generate faster code.

what coding practices available may enable compiler/optimizer generate faster code?

  • identifying platform , compiler use, appreciated.
  • why technique seem work?
  • sample code encouraged.

here related question

[edit] question not overall process profile, , optimize. assume program has been written correctly, compiled full optimization, tested , put production. there may constructs in code prohibit optimizer doing best job can. can refactor remove these prohibitions, , allow optimizer generate faster code?

[edit] offset related link

write local variables , not output arguments! can huge getting around aliasing slowdowns. example, if code looks like

void dosomething(const foo& foo1, const foo* foo2, int numfoo, foo& barout) {     (int i=0; i<numfoo, i++)     {          barout.munge(foo1, foo2[i]);     } } 

the compiler doesn't know foo1 != barout, , has reload foo1 each time through loop. can't read foo2[i] until write barout finished. start messing around restricted pointers, it's effective (and clearer) this:

void dosomethingfaster(const foo& foo1, const foo* foo2, int numfoo, foo& barout) {     foo bartemp = barout;     (int i=0; i<numfoo, i++)     {          bartemp.munge(foo1, foo2[i]);     }     barout = bartemp; } 

it sounds silly, compiler can smarter dealing local variable, since can't possibly overlap in memory of arguments. can avoid dreaded load-hit-store (mentioned francis boivin in thread).


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -