简体   繁体   English

头文件包含和性能注意事项c ++

[英]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? 在一个文件中具有所有必需标头的应用程序与不具有所有必需标头的应用程序之间是否存在性能差异?

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. 不要包含不需要的标头。

So you are asking what is the difference between having one big header and 5 small headers in code? 那你要问的是在代码中有一个大标题和5个小标题有什么区别? 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. 当编译标题的主体添加到“include”行的位置时,#include只修改源代码并且在运行时不会使应用程序变慢。 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. 这不会影响性能,但是在制作大型项目时,您需要多个标题,而不是制作大标题,原因与不在一个源代码中编写整个应用程序相同。

If you refer to static headers that you include with #include , then there is no difference at run-time. 如果您引用包含#include静态标头,那么在运行时没有区别。 The compiler will combine all the "information" in these headers into a single program. 编译器会将这些头中的所有“信息”组合到一个程序中。

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 (ie 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. 请注意,如果您的代码受到这些更改的影响,则可能容易受到实际破坏的影响,例如重新排序更改在使用点可见的模板特化集。

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

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