前向声明对编译时间有多大影响? [英] How much do forward declarations affect compile time?

查看:22
本文介绍了前向声明对编译时间有多大影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一些研究或经验数据非常感兴趣,这些研究或经验数据显示了两个相同的 C++ 项目之间编译时间的比较,除了一个尽可能使用前向声明而另一个不使用.

I am very interested in some studies or empirical data that shows a comparison of compilation times between two c++ projects that are the same except one uses forward declarations where possible and the other uses none.

与完整包含相比,前向声明对编译时间的改变有多大?

How drastically can forward declarations change compilation time as compared to full includes?

#include "myClass.h"

对比

class myClass;

是否有任何研究对此进行检验?

我意识到这是一个模糊的问题,很大程度上取决于项目.我不希望有一个硬性数字来回答.相反,我希望有人能够指导我进行有关此的研究.

I realize that this is a vague question that greatly depends on the project. I don't expect a hard number for an answer. Rather, I'm hoping someone may be able to direct me to a study about this.

我特别担心的项目有大约 1200 个文件.每个 cpp 平均包含 5 个标头.每个标题平均包含 5 个标题.这回归了大约 4 个深度.似乎对于每个编译的 cpp,必须打开和解析大约 300 个头文件,有些是多次.(包含树中有很多重复项.)有保护,但文件仍然打开.每个 cpp 都是用 gcc 单独编译的,所以没有头缓存.

The project I'm specifically worried about has about 1200 files. Each cpp on average has 5 headers included. Each header has on average 5 headers included. This regresses about 4 levels deep. It would seem that for each cpp compiled, around 300 headers must be opened and parsed, some many times. (There are many duplicates in the include tree.) There are guards, but the files are still opened. Each cpp is separately compiled with gcc, so there's no header caching.

为了确保没有人误解,我当然提倡在可能的情况下使用前向声明.然而,我的雇主已经禁止他们.我试图反驳那个立场.

To be sure no one misunderstands, I certainly advocate using forward declarations where possible. My employer, however, has banned them. I'm trying to argue against that position.

感谢您提供任何信息.

推荐答案

前向声明可以使代码更简洁更易于理解,这肯定是任何决策的目标.

Forward declarations can make for neater more understandable code which HAS to be the goal of any decision surely.

再加上当涉及到类时,2 个类很可能相互依赖,这使得在不使用前向声明而不导致噩梦的情况下有点困难.

Couple that with the fact that when it comes to classes its quite possible for 2 classes to rely upon each other which makes it a bit hard to NOT use forward declaration without causing a nightmare.

在头文件中同样前向声明类意味着您只需要在实际使用这些类的 CPP 中包含相关头文件.这实际上减少了编译时间.

Equally forward declaration of classes in a header means that you only need to include the relevant headers in the CPPs that actually USE those classes. That actually DECREASES compile time.

编辑:鉴于您上面的评论,我会指出包含头文件总是比转发声明慢.任何时候你包含一个头文件,你都需要从磁盘加载,通常只是为了发现头文件保护意味着什么都没有发生.这会浪费大量的时间,而且真的是一个非常愚蠢的规则.

Edit: Given your comment above I would point out it is ALWAYS slower to include a header file than to forward declare. Any time you include a header you are necessitating a load from disk often only to find out that the header guards mean that nothing happens. That would waste immense amounts of time and is really a VERY stupid rule to be bringing in.

编辑 2:硬数据很难获得.有趣的是,我曾经参与过一个对其标头包含不严格的项目,并且在 512MB RAM P3-500Mhz 上的构建时间大约为 45 分钟(这是前一段时间).在花费 2 周减少包含噩梦(通过使用前向声明)后,我设法在不到 4 分钟的时间内构建了代码.随后,尽可能使用前向声明成为一种规则.

Edit 2: Hard data is pretty hard to obtain. Anecdotally, I once worked on a project that wasn't strict about its header includes and the build time was roughly 45 minute on a 512MB RAM P3-500Mhz (This was a while back). After spending 2 weeks cutting down the include nightmare (By using forward declarations) I had managed to get the code to build in a little under 4 minutes. Subsequently using forward declarations became a rule whenever possible.

编辑 3:同样值得记住的是,在对代码进行小的修改时,使用前向声明有一个巨大的优势.如果整个商店都包含标头,那么对标头文件的修改可能会导致大量文件被重建.

Edit 3: Its also worth bearing in mind that there is a huge advantage from using forward declarations when it comes to making small modifications to your code. If headers are included all over the shop then a modification to a header file can cause vast amounts of files to be rebuilt.

我还注意到很多其他人都在赞美预编译头文件 (PCH) 的优点.他们有自己的位置,他们真的可以提供帮助,但他们真的不应该被用作适当的前向声明的替代品.否则对头文件的修改可能会导致重新编译大量文件(如上所述)以及触发 PCH 重建的问题.PCH 可以为诸如预先构建的库之类的东西提供巨大的胜利,但它们没有理由不使用正确的前向声明.

I also note lots of other people extolling the virtues of pre-compiled headers (PCHs). They have their place and they can really help but they really shouldn't be used as an alternative to proper forward declaration. Otherwise modifications to header files can cause issues with recompilation of lots of files (as mentioned above) as well as triggering a PCH rebuild. PCHs can provide a big win for things like libraries that are pre-built but they are no reason not to use proper forward declarations.

这篇关于前向声明对编译时间有多大影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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