Can comments affect performance?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
idlehandsrome18
Posts: 101
Joined: Tue Feb 24, 2009 12:54 am UTC
Location: The Internet

Can comments affect performance?

Postby idlehandsrome18 » Tue Feb 24, 2009 10:07 am UTC

I know it is probably a moot point, but can comments in your code have an impact on the system's compile and/or runitimes? I'm pretty sure that compilers don't allow comments to end up in byte code that is run by the system, so I suspect that only the compile time could be affected. Its possible that if your code has a huge amount of comments in it (like some crazy programmer decided they were going to put a Charles Dickens' novel in their code) that the compiler would lag, but I just don't know enough about them. Anyone have any ideas?
My webcomic:
Image

Notch
Posts: 318
Joined: Tue Dec 12, 2006 5:52 pm UTC
Location: Stockholm, Sweden
Contact:

Re: Can comments affect performance?

Postby Notch » Tue Feb 24, 2009 10:23 am UTC

In general, it has no effect at all.

But I've you've got an 18 TB comment in a 2 KB source file, it's going to affect compilation time just because it takes ages to access that much data. But it won't affect run time performance since it (always?) gets stripped away in compilation.
I don't know if there are any languages that keep comments after compilation. None of the ones I work with do.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Can comments affect performance?

Postby You, sir, name? » Tue Feb 24, 2009 10:47 am UTC

In a compiled language: No effect on runtime performance, negligible effect on compile time.
In an interpreted language with internal bytecode representation: No effect on runtime performance, negligible effect on startup time.
In an interpreted language without internal bytecode representation: Negligible effect on runtime performance.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Can comments affect performance?

Postby Berengal » Tue Feb 24, 2009 2:23 pm UTC

Notch wrote:In general, it has no effect at all.

But I've you've got an 18 TB comment in a 2 KB source file, it's going to affect compilation time just because it takes ages to access that much data. But it won't affect run time performance since it (always?) gets stripped away in compilation.
I don't know if there are any languages that keep comments after compilation. None of the ones I work with do.

Python's docstrings are a sort of comment that's kept in the runtime system. In fact, if you tell the interpreter to "optimize" the only thing it'll do is remove the docstrings. You could argue the docstrings aren't actually comments though, but an actual object attribute. I'd say they're both.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
idlehandsrome18
Posts: 101
Joined: Tue Feb 24, 2009 12:54 am UTC
Location: The Internet

Re: Can comments affect performance?

Postby idlehandsrome18 » Tue Feb 24, 2009 5:02 pm UTC

Yeah, I figured as much. The only instance I can think of where comments are actually interpreted by the compiler (to an extent) are javadocs, and those aren't even looked at during a normal compile routine.
My webcomic:
Image

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Can comments affect performance?

Postby Berengal » Tue Feb 24, 2009 5:26 pm UTC

Javadocs aren't the only comments interacted with by external tools. Literate Haskell even has a mode where the code is also a valid TeX document (the compiler ignores everything outside of \begin{code} \end{code} tags). Other languages have also adopted the /** */ convention (or something similar), at least unofficially, and created tools to parse them.

Just thought I'd say.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Re: Can comments affect performance?

Postby psykx » Tue Feb 24, 2009 5:38 pm UTC

You, sir, name? wrote:In an interpreted language without internal bytecode representation: Negligible effect on runtime performance.

I disagree with this, at work we write 30,000-50,000+ line php applications which are run 1000 of times a day on the live servers. We strip comments when putting code live because it makes an easily noticeable difference on serverload.

The rest I agree with though
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.

Grumpy Code Monkey
Posts: 99
Joined: Tue Feb 19, 2008 4:10 pm UTC
Location: Blue Texas

Re: Can comments affect performance?

Postby Grumpy Code Monkey » Tue Feb 24, 2009 7:10 pm UTC

I work exclusively with natively compiled languages (i.e., C and C++), and in my world, comments have zero effect on runtime performance and an immeasurably small effect on build times (just because the compiler has to at least read the text, even if it promptly throws it away). I can't speak to other environments.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Can comments affect performance?

Postby You, sir, name? » Tue Feb 24, 2009 7:37 pm UTC

psykx wrote:
You, sir, name? wrote:In an interpreted language without internal bytecode representation: Negligible effect on runtime performance.

I disagree with this, at work we write 30,000-50,000+ line php applications which are run 1000 of times a day on the live servers. We strip comments when putting code live because it makes an easily noticeable difference on serverload.

The rest I agree with though


PHP has an internal bytecode representation, so the performance penalty is at startup.[/nitpick] Yours is sort of a special case though. Applications with short runtime (on pageload) that are run over and over again are very sensitive to constant time overhead, like that from compilation.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: Can comments affect performance?

Postby Karrion » Wed Feb 25, 2009 11:50 pm UTC

idlehandsrome18 wrote:The only instance I can think of where comments are actually interpreted by the compiler (to an extent) are javadocs, and those aren't even looked at during a normal compile routine.

Berengal wrote:Javadocs aren't the only comments interacted with by external tools.

Java does actually have a special case where the compiler itself parses the doc comments - it looks for the @deprecated doc comment tag; this information is then included in the bytecode for the method.

(This behaviour is itself slightly deprecated since annotations in java 1.5, but the compiler still has to look for it to support older code)

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Can comments effect performance?

Postby PM 2Ring » Thu Feb 26, 2009 3:21 pm UTC

PostScript also has special comments that are used for document structuring, so they may be analyzed depending on what sort of interpreter is parsing the program.

http://en.wikipedia.org/wiki/Document_Structuring_Conventions

Document Structuring Conventions, or DSC, is a set of standards for PostScript, based on the use of comments, which primarily specifies a way to structure a PostScript file and a way to expose that structure in a machine-readable way. A PostScript file that conforms to DSC is called a conforming document.

The need for a structuring convention arises since PostScript is a Turing-complete programming language. There is thus no guaranteed method to do things like determining how many pages long a given document is or how large a given page is, or how to skip to a particular page. The addition of structure, with DSC comments exposing that structure, help provide a way for, e.g., an intelligent print spooler the ability to rearrange the pages for printing, or a way for a page layout program to find out the bounding box of a PostScript file used as a graphic image. Collectively, any such program that take PostScript files as input data is called a document manager.

Some DSC comments serve a second function, specifying a way to tell the document manager to do certain things, like inserting a font or other PostScript code (collectively called resources) into the file. DSC comments that serve this second function are more akin to preprocessing directives and are not purely comments, and thus require a functioning document manager to have an effect.

DSC is the basis for encapsulated PostScript, which are conforming documents with further restrictions.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Can comments effect performance?

Postby Berengal » Thu Feb 26, 2009 3:42 pm UTC

I'm willing to state that anything read by the compiler/interpreter isn't really a comment unless it has no effect on the resulting program (a compiler could spew out documentation files as well as the program for example). This includes java's "deprecated" annotation (also when used in comment form), hashbangs in scripts, docstrings in python etc.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Can comments effect performance?

Postby qbg » Thu Feb 26, 2009 3:53 pm UTC

Closest thing to comments effecting performance that I can think of: :lol:

Before:

Code: Select all

void inner_loop() {
  volatile int a = 0;
  for (int i = 0; i < 1000000; ++i) {++a;}
}


After:

Code: Select all

void inner_loop() {
  /*
  volatile int a = 0;
  for (int i = 0; i < 1000000; ++i) {++a;}
  */
}

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Can comments effect performance?

Postby Berengal » Thu Feb 26, 2009 4:38 pm UTC

You should know there is currently a temporary wordfilter in effect for "affect". Your post just justified that filter. ;)
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Quizatzhaderac
Posts: 1798
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: Can comments affect performance?

Postby Quizatzhaderac » Sun Mar 01, 2009 2:51 am UTC

idlehandsrome18 wrote:Its possible that if your code has a huge amount of comments in it (like some crazy programmer decided they were going to put a Charles Dickens' novel in their code) that the compiler would lag, but I just don't know enough about them.
Comments are usually taken out during what's called "lexical analysis" which takes only a handful of machine instructions per character in well designed compilers. If you put War and Peace into the comments of a C program the difference in compilation time would be too small to be noticed by a human on any PC made this decade.
The thing about recursion problems is that they tend to contain other recursion problems.

Carnildo
Posts: 2023
Joined: Fri Jul 18, 2008 8:43 am UTC

Re: Can comments affect performance?

Postby Carnildo » Sun Mar 01, 2009 6:26 am UTC

Quizatzhaderac wrote:
idlehandsrome18 wrote:Its possible that if your code has a huge amount of comments in it (like some crazy programmer decided they were going to put a Charles Dickens' novel in their code) that the compiler would lag, but I just don't know enough about them.
Comments are usually taken out during what's called "lexical analysis" which takes only a handful of machine instructions per character in well designed compilers. If you put War and Peace into the comments of a C program the difference in compilation time would be too small to be noticed by a human on any PC made this decade.

Only on the second compile. The first time you compile it, you'll get a noticable blip as it reads the file into the disk cache.

User avatar
Quizatzhaderac
Posts: 1798
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: Can comments affect performance?

Postby Quizatzhaderac » Sun Mar 01, 2009 6:05 pm UTC

I actually tried it to make sure I wasn't full of it. I took an existing c++ project of mine and added 10,000 lines of comments to the file with my main. When I an make it still produced the .o for my main file before I took my finger off the enter key. The disk read probably wasn't an issue because the file was stored contigously on the disk. In this particular scenerio we're talking about files that are being opened anyway.
The thing about recursion problems is that they tend to contain other recursion problems.

Soljer
Posts: 29
Joined: Fri Feb 27, 2009 6:31 pm UTC

Re: Can comments affect performance?

Postby Soljer » Fri Mar 06, 2009 5:04 am UTC

In java, unicode escapes are converted to actual characters - even in comments.

Thus, if you used the appropriate unicode escapes, you could actually end a block comment early (or get a new line for single-line comments) and execute commented-out code. I doubt that's the kind of answer you were looking for, though.

User avatar
idlehandsrome18
Posts: 101
Joined: Tue Feb 24, 2009 12:54 am UTC
Location: The Internet

Re: Can comments affect performance?

Postby idlehandsrome18 » Fri Mar 06, 2009 10:20 pm UTC

Soljer wrote:In java, unicode escapes are converted to actual characters - even in comments.

Thus, if you used the appropriate unicode escapes, you could actually end a block comment early (or get a new line for single-line comments) and execute commented-out code. I doubt that's the kind of answer you were looking for, though.

Interesting. Yeah, the spirit of the post was about whether some programmer who comments a lot is actually hurting their program, not about all kinds of extreme cases. But all the responses are very interesting and enlightening, a programmer should know how their compiler works.
My webcomic:
Image

User avatar
Quizatzhaderac
Posts: 1798
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: Can comments affect performance?

Postby Quizatzhaderac » Fri Mar 06, 2009 11:50 pm UTC

Soljer wrote:In java, unicode escapes are converted to actual characters - even in comments.

Thus, if you used the appropriate unicode escapes, you could actually end a block comment early (or get a new line for single-line comments) and execute commented-out code. I doubt that's the kind of answer you were looking for, though.

Java also keeps the string literals you use in your code to identify class, method and class memeber names. Even after the just-in-time assembely. But I think unless you actually use them via the reflex package that's just a negligible effect on load time/memory use.
The thing about recursion problems is that they tend to contain other recursion problems.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 4 guests