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

后端 未结 11 1814
半阙折子戏
半阙折子戏 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:01

    1. Writing fewer lines of code tends to be better for a bunch of reasons. For example, the less code you have, the less chance for bugs. See for example Paul Graham's essay, "Succinctness is Power"
    2. Notwithstanding that, the level reached by Code Golf is usually far beyond what makes sense. In Code Golf, people are trying to write code that is as short as possible, even if they know that it's less readable.
    3. Efficiency is a much harder thing to decide. I'm guessing that less code is usually more efficient, but there are many cases where this isn't true.

    So to answer the real question, why do we even have Code Golf competitions which aim at a low character count, if that's not a very important thing?

    Two reasons:

    Making code as short as possible means you have to be both clever, and know a language pretty well to find all kinds of tricks. This makes it a fun riddle.

    Also, it's the easiest measure to use for a code competition. Efficiency, for example, is very hard to measure, especially using many different languages, especially since some solutions are more efficient in some cases, but less in others (big input vs small). Readability: that's a very personal thing, which often leads to heated debates.

    In short, I don't think there is any way of doing Code Golf style competitions without using "shortness of code" as the criterion.

提交回复
热议问题