-4

Which one is faster?

Assume an average compiler for C++.

///////////////////////
Code A:

int a,b,c;
///////////////////////
Code B:

int a;
int b;
int c;
///////////////////////

It is also said that the compiler ignores comments completely. Like-

"// Comment"
OR
/* Comment*/

Assume your program has a comment block of a million lines. I'm sure the compiler reads for " / * " first and searches for the " * / " and identifies all the text between these two as comments. But doesn't that mean the compiler is iterating through million lines of code to find the " * / " after it encounters the " * / " ??

TLDR;

My questions are:-

  • 1) Is Code A faster or Code B??? Does short code mean less compilation time?
  • 2) Do comments contribute to compilation time?
  • 3) How can I learn to optimize my code for best performance?
kotAPI
  • 1,073
  • 2
  • 13
  • 37
  • 1
    what are you talking about? you have nothing to optimize there – BЈовић Mar 27 '14 at 11:51
  • You can benchmark each, but I can already tell you that you might be disappointed with the results. – chris Mar 27 '14 at 11:52
  • You mean to optimize your code for compiler execution time? – MatthiasB Mar 27 '14 at 11:52
  • 1
    There's nothing to optimize HERE. I am asking a few general queries I have. About the comments, the kind of code you write etc. – kotAPI Mar 27 '14 at 11:52
  • Comments cannot contribute to execution time since they're discarded. Between the two styles of variable declaration, you'll see no difference. – mah Mar 27 '14 at 11:53
  • I guess you could clarify this in your question, as a lot of people are thinking of execution time of the program – MatthiasB Mar 27 '14 at 11:54
  • 1
    You can have a look at this question, it shows how to measure it yourself using gcc: http://stackoverflow.com/questions/6390532/debugging-gcc-compile-times/6390569#6390569 Unfortunately i cannot find concrete numbers, but I guess that the initial step takes little time compared to the rest of the compilation. – MatthiasB Mar 27 '14 at 11:58
  • "Assume your program has a comment block of a million lines." - Why? Comments are for people, and no-one has the time to read a million lines. The time taken to ignore a sensible comment will be negligible. – Mike Seymour Mar 27 '14 at 12:17
  • @MikeSeymour, the question wasn't "Who'd read the million lines" It was "what would happen to the compilation time if there were amillion lines" – kotAPI Mar 27 '14 at 16:00

3 Answers3

5
  1. No, short code does not mean less execution time. For example see here
  2. No, comments do not change execution time of your code. (I would be quite surprised if you even noticed an increased compile-time)
  3. There are several talks online looking at this. Most importantly: Measure everything!
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
2

They will be exactly the same code, and the same speed. A variable declaration won't be generally an instruction for the compiler to do something, only a declaration for him, to "handle stackpointer-8 byte as an unsigned integer". The is no difference where if you declare these variables in the same line of not.

The answers:

  1. Generally not.
  2. No, they will be eliminated in the first stage of the compilation already.
  3. Too broad. Generally you should use optimized code with efficient algorithms.
peterh
  • 11,875
  • 18
  • 85
  • 108
  • What if the comments are over million lines? Like I said. The compiler needs to search for both the " / * " and " * / " to identify the characters between these two as comments right? Doesn't that mean the compiler is scanning through all the characters in between to find the " * / " to finalize this as a comment block? – kotAPI Mar 27 '14 at 11:53
  • 1
    @kotAPI it's a simple concept... comments are not part of the executable code. If they're no longer there then they can't affect it. Excessive comments might make it take longer to _compile_ (but you'd be unlikely to notice it). – mah Mar 27 '14 at 11:54
  • @mah Ahh, that's the answer I was expecting, I know comments don't add to executing time much, I wanna know if it takes hella lotta time if the comment blocks are over a million lines, is it going to take a lot of time? – kotAPI Mar 27 '14 at 11:55
  • 1
    You are not talking about execution time. The compiler will remove them from the code that is produced. You are talking about `compilation time`... in which case they will be removed the first pass, with the actual deletion time being so insignificant that you will never notice. For Q3 - write your application first using the best design you can think of. THEN optimise by testing your code with a profiling tool. – Dennis Mar 27 '14 at 11:55
  • @Dennis Oops , sorry my bad. I meant compilation time in that case. Thanks for the reply btw – kotAPI Mar 27 '14 at 11:56
  • @kotAPI In such extreme case you will have faster compilation, but exactly the same running time. Your question isn't about this, it is clearly about runtime. – peterh Mar 27 '14 at 11:57
  • @kotAPI you keep referring to "execution time" and "it". I guess from context, you mean things related to the compilation process... but you're not doing a great job of describing that since mostly people don't care about such a trivial detail. When people do care about it, they know that changing the source being compiled isn't going to get any measurable benefit... it's changing the build process itself that will matter (but only on very large projects). – mah Mar 27 '14 at 11:58
  • @mah Yeah, I totally meant compilation time. I was just thinking about it this morning "What if i added a million line comment"? What would happen to my program? Anyway thanks for the brief description, despite of getting down voted, I got my answer and I'm happy. – kotAPI Mar 27 '14 at 12:00
2

1) No, not as a rule. 'Sleep(24*60*60*1000)' takes a day to execute.

2) No, of course not. The 'worst' thing that could happen is that building takes slightly longer, and I mean 'slightly' - computers are really fast.

3) Learn much, much more about the compilation process and computers in general.

Martin James
  • 24,453
  • 3
  • 36
  • 60