views:

261

answers:

5

What is the difference between including a file from a header file or from a source file in C++ in regards to performance during compilation?

What are the reasons to include files in the source file and not in the header file (except when absolutely required)?

Does the one (header) affect compile time and the other (source file) affect link time?

+1  A: 

For any performance question the real answer is measure it yourself - your environment wil be different to anyone else's.

For this case I would guess it should be the same unless precompiled headers are involved - if they are then if the include is in the precompilation then it will be quicker as it only gets compiled once.

Mark
+1  A: 

As Mark says, measure it in your environment. Generally speaking, if you include it in the source file, it is only read where included and needed. If you include it in the header file, and this header file gets included by quite a lot of other source files, compilation time will increase. That's also the reason why you should use forward declarations in header files wherever possible instead of including the class' header file.

jhwist
+11  A: 

When you include a file in either place, you can think of it as being expanded in the file, which then has to be processed by the preprocessor and compiler.

When you include something in your header, every client that includes your header inherits those includes. Thus, unnecessarily including files in headers has the potential to expand several translation units, which adversely affects performance.

It is good practice to limit header includes to those required to declare the class. Beyond limiting includes to those types used in a class, you may also use forward declarations in lieu of includes for types only used in the class interface by pointer or reference.

In my experience this performance impact is usually not noticeable. It likely comes into play in very large projects or widely used headers.

Adam
+1  A: 

People used to only include headers from the source file (.cpp/other extensions) to reduce the compilation time, because otherwise it generated a cascade of headers. Nowadays it's not a concern anymore, and including the headers where they are actually needed (even other headers) may avoid you to have to include many headers everytime in your sources...

http://www.icce.rug.nl/documents/cplusplus/cplusplus07.html#an973 (for a more elaborate answer)

RedGlyph
+1  A: 

As Adam wrote, including headers in a header makes your compilation units larger, which costs performance. But this is only noticeable in large projects. This is very important for example for the OS headers, like <windows.h>. That's why precompiled headers and WIN32_LEAN_AND_MEAN have been invented.

But there is another build performance problem if you include headers unnecessarily in other header: you may need to rebuild more if the header changes.

consider:

// A.h
class A
{
    ...
}

// B.h
#include "A.h"
class B
{
    A *_a;
    ...
}

If you change A.h, the IDE will recompile sources that include B.h even if they don't use class A. But if you change B.h to:

// B.h
class A; // forward declaration, declared in "A.h"
class B
{
    A *_a;
    ...
}

This will no longer be necessary. This can make a noticeable difference even for smaller projects.

oefe