Writing shorter code/algorithms, is more efficient (performance)?

后端 未结 11 1752
半阙折子戏
半阙折子戏 2021-02-19 15:19

After coming across the code golf trivia around the site it is obvious people try to find ways to write code and algorithms as short as the possibly can in terms of characters,

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 16:06

    Whitespace has no effect on performance. So code like that is just silly (or perhaps the golf score was based on the character count?). The number of lines also has no effect, although the number of statements can have an effect. (exception: python, where whitespace is significant)

    The effect is complex, however. It's not at all uncommon to discover that you have to add statements to a function in order to improve it's performance.

    Still, without knowing anything else, bet that more statements is a larger object file, and a slower program. But more lines doesn't do anything other than make code more readable up to a point (after which adding more lines makes it less readable ;)

提交回复
热议问题