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

后端 未结 30 1977
一个人的身影
一个人的身影 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:24

    Most modern compilers should do a good job speeding up tail recursion, because the function calls can be optimized out.

    Example:

    int fac2(int x, int cur) {
      if (x == 1) return cur;
      return fac2(x - 1, cur * x); 
    }
    int fac(int x) {
      return fac2(x, 1);
    }
    

    Of course this example doesn't have any bounds checking.

    Late Edit

    While I have no direct knowledge of the code; it seems clear that the requirements of using CTEs on SQL Server were specifically designed so that it can optimize via tail-end recursion.

提交回复
热议问题