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

后端 未结 11 1801
半阙折子戏
半阙折子戏 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条回答
  •  庸人自扰
    2021-02-19 16:17

    It makes a difference if you're talking about little academic-style algorithms or real software, which can be thousands of lines of code. I'm talking about the latter.

    Here's an example where a reasonably well-written program was speeded up by a factor of 43x, and it's code size was reduced by 4x.

    "Code golf" is just squeezing code, like cramming undergraduates into a phone booth. I'm talking about reducing code by rewriting it in a form that is declarative, like a domain-specific-language (DSL). Since it is declarative, it maps more directly onto its requirements, so it is not puffed up with code that exists only for implementation's sake. That link shows an example of doing that. This link shows a way of reducing size of UI code in a similar way.

    Good performance is achieved by avoiding doing things that don't really have to be done. Of course, when you write code, you're not intentionally making it do unnecessary work, but if you do aggressive performance tuning as in that example, you'd be amazed at what you can remove.

提交回复
热议问题