How expensive it is for the compiler to process an include-guarded header?

I read an FAQ about this the other day... first off, write the correct headers, i.e. Include all headers that you use and don't depend on undocumented dependencies (which may and will change).

Up vote 0 down vote favorite 1 share g+ share fb share tw.

If the latter is true an engineering effort would be better spent creating more, lightweight headers instead of less. So how long does it take for a modern compiler to handle a header that is effectively include-guarded out? At what point would the inclusion of such headers become a hit on compilation performance?

(related to this question) performance include preprocessor compiler-optimization link|improve this question asked Jul 13 '11 at 23:21fbrereto15.8k33080 90% accept rate.

I read an FAQ about this the other day... first off, write the correct headers, i.e. Include all headers that you use and don't depend on undocumented dependencies (which may and will change). Second, compilers usually recognize include guards these days, so they're fairly efficient.

However, you still need to open a lot of files, which may become a burden in large projects. One suggestion was to do this: Header file: // file. Hpp #ifndef H_FILE #define H_FILE /* ... */ #endif Now to use the header in your source file, add an extra #ifndef: // source.

Cpp #ifndef H_FILE # include #endif It'll be noisier in the source file, and you require predictable include guard names, but you could potentially avoid a lot of include-directives like that.

– Karoly Horvath Jul 13 '11 at 23:31 @yi_H: The second code block goes inside the source file (I edited that in). The point is that if file. Hpp has already been included, you never need to open file.

Hpp to find its include guard. – Kerrek SB Jul 13 '11 at 23:33 I don't know how compilers work but if I wrote one I would store in a flag whether the header is guarded, if so and the constant still defined then there is no need to open and include it again. Do compilers use such kind of mechanism?

If so, the whole extra proposed guards are pointless. – Karoly Horvath Jul 13 '11 at 23:36 It's quite possible that they do. In which case you'd already be doing as well as you could, unless you're willing to restructure the headers and create forward-declaration-only headers, I guess.

– Kerrek SB Jul 13 '11 at 23:38 Thanks for the comment. Any suggestions on how to go about timing the compiler's ability to deal with include-guarded headers v. "pre-guarding" them as you describe above?

– fbrereto Jul 13 '11 at 23:43.

Assuming C/C++, simple recompilation of header files scales non-linearly for a large system (hundreds of files), so if compilation performance is an issue, it is very likely down to that. At least unless you are trying to compile a million line source file on a 1980s era PC... Pre-compiled headers are available for most compilers, but generally take specific configuration and management to work on non system-headers, which not every project does. See for example: cygnus-software.com/papers/precompiledhe... 'Build time on my project is now 15% of what it was before!

' Beyond that, you need to look at the techniques in: amazon.com/exec/obidos/ASIN/0201633620/q... Or split the system into multiple parts with clean, non-header-based interfaces between them, say . NET components.

I discovered PCHs recently (while fighting with Boost) and I love them, but I got lots of negative reactions from people when asked about the topic... – Kerrek SB Jul 13 '11 at 23:52.

The answer: It can be very expensive! I found an article where someone had done some testing of the very issue addressed here, and am astonished to find you can increase your compilation time under MSVC by at least an order of magnitude if you write your include guards properly: bobarcher.org/software/include/index.html The most astonishing line from the results is that a test file compiled under MSVC 2008 goes from 5.48s to 0.13s given the right include guard methodology.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions