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

后端 未结 30 1905
一个人的身影
一个人的身影 2020-12-02 03:24

Many years ago, C compilers were not particularly smart. As a workaround K&R invented the register keyword, to hint to the compiler, that maybe it woul

30条回答
  •  Happy的楠姐
    2020-12-02 04:24

    1. Use the most local scope possible for all variable declarations.

    2. Use const whenever possible

    3. Dont use register unless you plan to profile both with and without it

    The first 2 of these, especially #1 one help the optimizer analyze the code. It will especially help it to make good choices about what variables to keep in registers.

    Blindly using the register keyword is as likely to help as hurt your optimization, It's just too hard to know what will matter until you look at the assembly output or profile.

    There are other things that matter to getting good performance out of code; designing your data structures to maximize cache coherency for instance. But the question was about the optimizer.

提交回复
热议问题