Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do multiple declarations of the same type affect compile time?

Tags:

c++

c

I was wondering whether declaring multiple things of the same type affects the compile time, such as below.

void a(),
     b(),
     c();

vs

void a();
void b();
void c();
like image 755
Jonascone Avatar asked Jun 25 '13 11:06

Jonascone


3 Answers

I would be surprised if it didn't matter at all since the compiler will execute different code for the two cases, but it's impossible to guess which one would be faster, and whether one would be consistently faster than the other.

I would also be surprised if it were possible to measure the difference in any meaningful way, as it would most likely never be a question of more than a few microseconds.

If you're having problems with compilation time, declaration style is not the cause.

like image 72
molbdnilo Avatar answered Sep 24 '22 15:09

molbdnilo


This answer discusses why the proposed code makes little difference to the compile time, which none of the other answers currently address.

Typically, some of the basic structure of a modern compiler is:

  • There is a part that reads each character, matches the characters to simple patterns (such as “a sequence of letters and numbers starting with a letter” or ”+” or “/* text */”, and packages those into tokens with extra data (identifier with text for name, operator of type +, white space). This part is called the lexical analyzer.
  • There is a part that receives the tokens and analyzes their syntactic structure.
  • There are other parts that do not concern us here.

The part that analyzes the syntactic structure does something like this: When it recognizes a() inside a declaration such as void a(), it calls a routine elsewhere in the compiler that registers a as an identifier with type function returning void. This happens when a() is recognized, when b() is recognized, and when c() is recognized.

The important thing in this case is that the same thing happens with both proposed code sequences: a(), b() and c() are all recognized, and the same sequence of routine calls is made to register those identifiers. From then on, the processing of them is identical. In typical compilers, there will be no difference in the way a(), b(), and c() are treated.

This means the only difference in the compiler’s processing of these code sequences is in the lexical analyzer and in the syntax processor. For one of the code sequences, there are a few more characters and a few more tokens, so that code sequence is likely to take slightly longer to process. However, with the speed of today’s computers, that amount of time is minuscule.

It is possible that slight differences in the processing can have cascading effects, if they happen to affect what is stored in processor cache or what memory is allocated and freed. However, these are completely incidental, the same way that, if a mechanic works on one car and happens to move their wrench to a different shelf while doing it, it can affect how long it takes them to work on the next car because they have to walk over to the shelf to get the wrench. It is just happenstance and not a meaningful cause-and-effect.

like image 43
Eric Postpischil Avatar answered Sep 26 '22 15:09

Eric Postpischil


Not appreciably. Don't worry about it.

like image 39
Jeremy Roman Avatar answered Sep 26 '22 15:09

Jeremy Roman