Sebi
Sebi

Reputation: 4522

Header files inclusion and performance considerations c++

Assuming all headers have include guards set appropriately, are there any means of improving run time performance of an application by simply altering these headers?

Is there any difference, performance wise, between an application that has all the needed headers in one file and an application that doesn't?

Upvotes: 0

Views: 752

Answers (4)

Ben Voigt
Ben Voigt

Reputation: 283634

If you end up changing the order in which the headers are included, AND your headers are doing things which headers ought not to do (i.e. defining objects and not merely types and functions), then you could change memory locality and affect runtime performance.

Or, if you have redundant declarations of inline-defined functions (which is also not a great idea), and you reorder the headers so that the definition becomes visible to more or fewer call sites, then it's possible for runtime performance to change (although most optimizers should be able to inline upwards).

Note that if your code is subject to these changes, it's probably vulnerable to actual breakage, such as reordering changing the set of template specializations which are visible at the point of use.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753675

Run-time efficiency

The sets of headers included or not included really have no effect on the efficiency of the resulting application at run time. You should include the headers that declare the functions you use. If those headers provide inline function definitions, for example, then you may get some performance boost by comparison with (hypothetical alternative) headers that don't provide inline function declarations, but you still need to include the header so that is controlled by the people providing the headers.

Compile-time efficiency

I first assumed this was what you're after, in part because of the mention of header include guards which are a purely compilation-related issue.

If the headers are set up with include guards, you can still improve efficiency of compilation (with no effect on the product) by following the rules below:

  • Don't include the same header twice at the source code level.

    #include <stdio.h>
    
    ...40 other project specific includes...
    
    #include <stdio.h>
    

    (Yes, I've seen it in real code; I've fixed it, often. And, as noted in the comments, it isn't an incredibly bad performance issue, but it is messy in the code.)

  • Don't include headers that aren't needed.

Upvotes: 2

Cantfindname
Cantfindname

Reputation: 2138

If you refer to static headers that you include with #include, then there is no difference at run-time. The compiler will combine all the "information" in these headers into a single program.

Upvotes: 0

Dydzio
Dydzio

Reputation: 84

So you are asking what is the difference between having one big header and 5 small headers in code? When compiling the body of header is added at the place of "include" line so #include just modifies source code and does not make application slower when running. That does not affect performance but when making big project you will want multiple headers, instead of making big one, for same reason as not writing whole application in one source code.

Upvotes: 0

Related Questions