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

后端 未结 11 1756
半阙折子戏
半阙折子戏 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 15:59

    I don't believe that Code Golf has any practical significance. In practice, readable code is what counts. Which in itself is a conflicting requirement: readable code should be concise, but still easy to understood.

    However, I would like to answer your question yet differently. Usually, there are fast and simple algorithms. However, if the speed is top priority, things can get complex real fast (and the resulting code will be longer). I don't believe that simplicity equals speed.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 16:03

    I hope this does not become a flame war. Good code has many attributes, including:

    1. Solving the use-case properly.
    2. Readability.
    3. Maintainability.
    4. Performance.
    5. Testability.
    6. Low memory signature.
    7. Good user interface.
    8. Reusability.

    The brevity of code is not that important in 21st century programming. It used to be more important when memory was really scarce. Please see this question, including my answer, for books referencing the attributes above.

    0 讨论(0)
  • 2021-02-19 16:03

    Writing "code golf" solutions are often to do with showing how "clever" you are in getting the job done in the most succinct way even at the expense of readability. Quite often, however, more verbose code including, for example, memoization of function results, can be faster. Code size can matter for performance, smaller blocks of code can fit in the L1 CPU cache but this is an extreme case of optimization and a faster algorithm will most always be better. "Code Golf" code is not like production code - always write for clarity & readability of the solution rather than terseness if anyone, including yourself, ever intend to read that code again.

    0 讨论(0)
  • 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 ;)

    0 讨论(0)
  • 2021-02-19 16:08

    This is from "10 Commandments for Java Developers"

    Keep in Mind - "Less is more" is not always better. - Code efficiency is a great thing, but > in many situations writing less lines of code does not improve the efficiency of that code.

    This is (probably) true for all programming languages (though in assembly it could be different).

    0 讨论(0)
提交回复
热议问题