简体   繁体   English

在C / C ++中包含未使用的头文件会有性能影响吗?

[英]Will there be a performance hit on including unused header files in C/C++?

I have a project where each C/C++ file uses a bunch of header files. 我有一个项目,其中每个C / C ++文件使用一堆头文件。 But about 70-80% of the header files that each C/C++ file uses is the same. 但是每个C / C ++文件使用的头文件大约有70-80%是相同的。 So to make my code more readable, I am planning to include all the headers that I will need in the project into a single header file say common_headers.h and include this in all my C/C++ files like this: 因此,为了使我的代码更具可读性,我计划将项目中需要的所有头文件包含在单个头文件中,例如common_headers.h并将其包含在我的所有C / C ++文件中,如下所示:

#include "common_headers.h"

Now this will include all the necessary headers but also few extra headers that will not be used by an individual file. 现在,这将包括所有必需的标题,但也包含一些单独文件不会使用的额外标题。 I want to know if doing this way, would it hit the performance at run time by any chance? 我想知道如果这样做,是否会在运行时以任何机会达到性能?

I am fine with a few milliseconds extra delay for compiling the code, but I want to know if this will impact my runtime performance? 我很好,有几毫秒的额外延迟来编译代码,但我想知道这是否会影响我的运行时性能?

Description of headers used: 使用的标题说明:

  1. Most of them are standard C/C++ headers. 其中大多数是标准的C / C ++标头。
  2. The user defined headers have inline template functions in them . 用户定义的标题中包含内联模板函数。
  3. No static functions in user defined headers. 用户定义的标头中没有静态函数。

This is my Compiler: g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3) 这是我的编译器: g ++(GCC)4.4.7 20120313(Red Hat 4.4.7-3)

COMPILATION: 汇编:

If something is included then it has to analyzed even if it will never be actually compiled and linked, so compilation time will increase for sure - Do not include unused headers . 如果包含某些内容,那么即使它实际上永远不会被编译和链接也必须进行分析,因此编译时间肯定会增加 - 不要包含未使用的标头

RUNTIME: 运行:

It has been already mentioned by @DonReba that unused headers may include some pragma directives that can change the resulting executable, but usually it won't be the case. @DonReba已经提到过,未使用的头文件可能包含一些可以更改生成的可执行文件的pragma伪指令,但通常情况并非如此。

Most of the unused functions and declaraions will be optimized out, excluding some specific cases - Do unused functions get optimized out? 大多数未使用的函数和声明都将被优化,不包括某些特定情况 - 未使用的函数是否已经过优化? . The resulting exe may become a bit bigger, but those functions and variables won't be used, so overall impact will be minimal. 生成的exe可能会变得更大,但不会使用这些函数和变量,因此总体影响将会很小。 - - Nonetheless, do not include unused headers . - - 但是,不要包含未使用的标头

SUMMARY: 摘要:

If you can modify your source code to not include anything unneeded - modify it. 如果您可以修改源代码以不包含任何不需要的内容 - 请对其进行修改。

Personnaly I prefer to have self-contained modules(headers), that include everything they need - nothing more, nothing less. Personnaly我更喜欢拥有自包含的模块(标题),包括他们需要的一切 - 仅此而已。 Such modules can be added and removed without hindsight and possibility that some unneeded dependency has been left. 可以在没有后见之明的情况下添加和删除这些模块,并且可能存在一些不需要的依赖性。 They are still not panacea, but coupled with attentiveness and some code analysis they will keep your program free from deadweight headers. 它们仍然不是灵丹妙药,但加上注意力和一些代码分析,它们将使您的程序免受无谓的标题。

EDIT: 编辑:

Precompiled headers: 预编译头文件:

Precompiled headers are used to reduce compilation time for often used, but rarely changed headers(system headers, huge project headers), so if those unused headers are included in the precompiled header, then the compilation time effect during subsequent compilations will be minimized. 预编译头用于减少常用但很少更改的头(系统头,大型项目头)的编译时间,因此如果预编译头中包含那些未使用的头,则后续编译期间的编译时间效应将最小化。 Still, all runtime issues, no matter how small they are, stay the same as for simple header includes. 尽管如此,所有运行时问题,无论它们有多小,都与简单的标题包括一样。

Short answer to the question asked: No. 对问题的简短回答: 不。

Long answer: 答案很长:

More headers mean marginally more chance of some problem appearing that might manifest as a performance issue, but it's really not a concern. 更多标题意味着出现一些可能表现为性能问题的问题的可能性略高,但实际上并不是一个问题。

Many consider it poor style to do as you plan, but there are also those that consider it acceptable. 许多人认为你的计划风格很差,但也有人认为这是可以接受的。

One of the key reasons for avoiding this style is that it will make it easier to get circular dependencies. 避免这种风格的一个关键原因是它将使得更容易获得循环依赖。

I would discourage it due to the compile time issue where I do not have pre-compiled headers. 由于编译时间问题,我没有预先编译的头文件,我会劝阻它。

Depends on compiler. 取决于编译器。 Today's most compilers are smart and use precompiled headers to improve performance. 今天大多数编译器都是智能的,并使用预编译头来提高性能。 I use GCC compiler which supports precompiled header and AFAIK does not affect performance. 我使用支持预编译头的GCC编译器,AFAIK不影响性能。

It is possible that inclusion of an extra header would produce different runtime code due to overriding a preprocessor directive. 由于覆盖了预处理程序指令,包含额外标头可能会产生不同的运行时代码。 But this would not be a normal situation. 但这不是正常情况。

Visual C++, GCC, and Clang support precompiled headers for improving compilation times for use cases like yours. Visual C ++,GCC和Clang支持预编译头文件,以改善像您这样的用例的编译时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM