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

后端 未结 30 1951
一个人的身影
一个人的身影 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条回答
  •  感情败类
    2020-12-02 04:14

    A dumb little tip, but one that will save you some microscopic amounts of speed and code.

    Always pass function arguments in the same order.

    If you have f_1(x, y, z) which calls f_2, declare f_2 as f_2(x, y, z). Do not declare it as f_2(x, z, y).

    The reason for this is that C/C++ platform ABI (AKA calling convention) promises to pass arguments in particular registers and stack locations. When the arguments are already in the correct registers then it does not have to move them around.

    While reading disassembled code I've seen some ridiculous register shuffling because people didn't follow this rule.

提交回复
热议问题