在C/C ++中包含未使用的头文件是否会对性能产生影响? [英] Will there be a performance hit on including unused header files in C/C++?

查看:287
本文介绍了在C/C ++中包含未使用的头文件是否会对性能产生影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中每个C/C ++文件都使用一堆头文件.但是每个C/C ++文件使用的头文件中大约70-80%是相同的.因此,为了使我的代码更具可读性,我计划将项目中需要的所有标头包含在一个单独的标头文件中,例如common_headers.h,并将其包含在我的所有C/C ++文件中,如下所示:

I have a project where each C/C++ file uses a bunch of header files. But about 70-80% of the header files that each C/C++ file uses is the same. 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:

#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?

使用的标题说明:

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

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

推荐答案

编译:

如果其中包含某些内容,则即使它永远不会被实际编译和链接,也必须对其进行分析,因此,确定的编译时间将会增加-请勿包括未使用的标头.

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.

运行时间:

@DonReba已经提到,未使用的标头可能包含一些pragma指令,这些指令可以更改生成的可执行文件,但通常情况并非如此.

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.

除某些特定情况外,大多数未使用的函数和声明将进行优化-未使用的功能会得到优化吗?.生成的exe可能会更大一些,但不会使用这些函数和变量,因此总体影响将很小. --不过,请勿包括未使用的标头.

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. - - Nonetheless, do not include unused headers.

摘要:

如果您可以将源代码修改为不包含不需要的任何内容,请对其进行修改.

If you can modify your source code to not include anything unneeded - modify it.

Personnaly我更喜欢拥有独立的模块(标头),其中包括它们需要的所有内容-仅此而已.可以添加和删除此类模块,而无需事后回想,也可以避免留下不必要的依赖.它们仍然不是灵丹妙药,但是加上专心和一些代码分析,它们将使您的程序摆脱无谓的头文件.

Personnaly I prefer to have self-contained modules(headers), that include everything they need - nothing more, nothing less. 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.

预编译头文件:

使用预编译头可以减少经常使用但很少更改的头(系统头,庞大的项目头)的编译时间,因此,如果这些未使用的头包含在预编译头中,则后续编译期间的编译时间效果将是最小化.尽管如此,所有运行时问题,无论它们有多小,都与简单标头包含项保持相同.

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.

这篇关于在C/C ++中包含未使用的头文件是否会对性能产生影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆