2

I think I have came across a few times when I reformat some segment of code, it performed the way I wanted it to other than the other poorly formatted segment of code? Does code format affect performance? Or is that a myth?

  • 3
    Get write some C code. Compile the code. Format the code. Compile the code. If the two binaries are the same, there is absolutely no difference. (Some languages imbed debugging information like line numbers which could confuse the simple check). – user40980 Nov 1 '14 at 23:47
  • 2
    Where have you come across claims that code format has an affect on program performance? (BTW, I am having trouble parsing the first sentence of your question). – Jay Elston Nov 2 '14 at 0:04
  • 1
    @JayElston This is actually a common misconception among newbie programmers who are only told the simplified explanation "the computer reads and executes your code" and weren't yet taught what actually happens behind the scenes when they click on "compile&run". – Philipp Nov 2 '14 at 0:21
  • 6
    Code formatting effects performance - your performance - the way how you perform when you have to read and understand it. – Doc Brown Nov 2 '14 at 6:16
  • 2
    Learn how to get performance. It's not what you think, and the skill will never leave you. – Mike Dunlavey Nov 3 '14 at 20:16
10

In a compiled language, any superfluous whitespaces, comments or other elements without syntactical meaning do not survive the tokenization step of the compilation, so it doesn't make a difference at all for the resulting binary (at least for the executable parts - some compilers might embed original sourcecode in the generated binary for debugging purposes, but those parts of the binary aren't executed).

In purely interpreted languages, the interpreter needs to parse all whitespaces. So formating can actually slow down the interpreter a tiny bit. But more advanced* interpreters usually compile the code in-memory to an optimized representation ("bytecode") before they execute it . In this step, whitespaces are usually also stripped, so it shouldn't matter for runtime either.

*in this case "more advanced" means "anything you would use in a 2014 production environment".

  • 2
    Don't tell that to the Espruino guys. They take the term "significant whitespace" to a whole new level. :) – Sedate Alien Nov 3 '14 at 0:35
5

To the compiler/interpreter it matters very little how well formatted and/or commented your code is. It only increases the parsing time by a very tiny amount because the parser has more characters to process (and mostly skip).

In most languages, the compiler/interpreter would be perfectly happy if you present your code as a single long line without any comments at all.

On the other hand, for human readers (including you in a few months time) it makes a world of difference if the code is well-formatted, well-commented and easily readable.
With well-written code, you can usually glance over it to get an idea of what the code is doing. If the code has poor or no formatting,then you really have to pay attention to understand the code which takes significantly more time and energy.

1

The only time I think format matters is with dynamically downloaded code - ie things like javascript where huge blocks of comments and/or very long variable names will slow down the download, but even then this does not affect the compile time. (and the download time can be fixed by minimising the javascript file).

For compiled files, the source is just a collection of symbols separated by whitespace. The compile doesn't care how these are formatted, a single long line would be just as good as a nicely formatted source file, as far as the compiler is concerned (admittedly some languages would care, eg Python that relies on formatting, particularly new line indentation, but even that doesn't affect the speed of compilation).

Formatting is for people, not for computers. Its important for that reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.