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

11

15

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, lines and total size, even if that means writing something like:

    //Code by: job
    //Topic: Code Golf - Collatz Conjecture
    n=input()
    while n>1:n=(n/2,n*3+1)[n%2];print n

So as a beginner I start to wonder whether size actually matters :D

It is obviously a very subjective question highly dependent on the actual code being used, but what is the rule of thumb in the real world.

In the case that size wont matter, how come then we don't focus more on performance rather than size?

Spur answered 8/3, 2010 at 6:58 Comment(0)
B
18

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.

Barny answered 8/3, 2010 at 7:5 Comment(6)
Thanks! i'll give those book a look regarding the topicSpur
+1 That's right, brevity really isn't that important today. In fact, it often leads to some really ugly code (e.g. nested ternary operators).Mayo
I'd phrase t slightly differently: brevity is only useful when it helps with some of the other goals. For example brevity can enhance readability and maintainability. Excessive brevity, however can equally well destroy both.Dunn
Brevity is paramount in situations where the code gets transmitted a lot over networks. Check the html of www.google.com! It's about bandwith, not memory.Chide
@Chide - you are very right. I believe also programs for real-time devices will be as short as possible. I guess you should optimize for the resource that is the bottleneck and not worry too much about other resources.Barny
@Yuval - yes, I touched upon it in my answer, but I don't know if anybody read it.Chide
A
10

A lot of good answers already about what's important versus what's not. In real life, (almost) nobody writes code like code golf, wtih shortened identifiers, minimal whitespace, and the fewest possible statements.

That said, "more code" does correlate with more bugs and complexity, and "less code" tends to correlate with better readability and performance. So all other things being equal, it's useful to strive for shorter code, but only in the sense of "these simple 30 lines of code do the same as that 100 complex lines of code".

Ablaut answered 8/3, 2010 at 7:17 Comment(0)
A
6

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.

Alp answered 8/3, 2010 at 7:5 Comment(0)
B
4

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 ;)

Bolshevist answered 8/3, 2010 at 7:6 Comment(3)
Oh i see your point. So the important thing is statements. How come whitespace is significant in python?Spur
Whitespace is significant in Python because the language designer decided it should be. I think it's intended to make using the language quicker and easier to write code in.Bolshevist
@Carlucho - it's actually to enforce some degree of formatting standards (ie. if you have an if statement, you must indent the body). It's based more on maximising ease of maintenance rather than the initial writing.Odette
S
4

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.

Spindry answered 8/3, 2010 at 7:22 Comment(1)
In some languages, it can show you sneaky and inventive ways of doing things, and every once in a while you may find something you like and (if well documented) makes your real code better. It's rare but it happens. If nothing else, code golf is a way to really get to know your language of choice.Corniculate
C
3

There are many aspects to performance. Performance can for example be measured by memory footprint, speed of execution, bandwith consumption, framerate, maintainability, supportability and so on. Performance usually means spending as little as possible of the most scarce resource.

When applied to networking, brevity IS performance. If your webserver serves a little javascript snippet on every page, it doesn't exactly hurt to keep the variable names short. Pull up www.google.com and view source!

Some times DRY is not helping performance. An example is that Microsoft has found that they don't want a to loop through an array unless it is bigger than 3 elements. String.Format has signatures for one, two and three arguments, and then for array.

There are many ways of trading one aspect for another. This is usually called caching.

You can for example trade memory footprint for speed of execution. For example by doing lookup instead of execution. It is just a matter of replacing () with [] in most popular languages. If you plan it so that the spaceship in your game can only go in a fixed number of directions, you can save on trigonometric function calls.

Or you can use a proxy server with a cache for looking up things over a network. DNS servers do this all the time.

Finally, if development team availability is the most scarce resource, clarity of code is the best bet for maintainability performance, even if it doesn't run quite as fast or is quite as interesting or "elegant" in code.

Chide answered 8/3, 2010 at 8:37 Comment(2)
sorry but, what does DRY stand for?Spur
Don't Repeat Yourself. It is considered foolish to copy and paste code around, for many reasons.Chide
P
2

Absolutely not. Code size and performance (however you measure it) are only very loosly connected. To make matter worse whats a neat trick on one chip/compiler/OS may very well be the worse thing you can do in another archictecture.

Its counter-intuitive but a clear well written simple as possible implmentation is often far more efficient than than a devious bag of tricks. Today's optimizing compilers like clear uncomplicated code just as much as humans and complex trickery can cause them to abandon thier best optimizing strategies.

Prowler answered 8/3, 2010 at 8:59 Comment(0)
P
2
  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.

Popgun answered 8/3, 2010 at 9:13 Comment(1)
+1 For mentioning that readability is mainly subjective (and afaik not measurable in any form).Mayo
M
1

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).

Mayo answered 8/3, 2010 at 8:35 Comment(3)
I didn't know there were 10 commandments for java... well one more religion cant hurt. can it?Spur
It is not true for code that gets transmitted over a network a million times a day.Chide
When I started programming in Java, I found this article on Javalobby. Since then it reminds me to comment my code, test it, not reinvent the wheel, etc.Mayo
F
1

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.

Formula answered 8/3, 2010 at 12:26 Comment(0)
P
1

The point of code golf is to optimise for one thing (source length), at the potential expense of everything else (performance, comprehensibility, robustness). If you accidentally improve performance that's a fluke - if you could shave a character off by doubling the runtime, then you would.

You ask "how come then we don't focus more on performance rather than size", but the question is based on a false premise that programmers focus more on code size than on performance. They don't, "code golf" is a minority interest. It's challenging and fun, but it's not important. Look at the number of questions tagged "code-golf" against the number tagged "performance".

As other people point out, making code shorter often means making it simpler to understand, by removing duplication and opportunities for obscure errors. That's usually more important than running speed. But code golf is a completely different thing, where you remove whitespace, comments, descriptive names, etc. The purpose isn't to make the code more comprehensible.

Prejudice answered 8/3, 2010 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.