什么技术可以用来加快C ++编译时间? [英] What techniques can be used to speed up C++ compilation times?

查看:141
本文介绍了什么技术可以用来加快C ++编译时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么技术可以用来加快C ++编译时间?



这个问题出现在Stack Overflow问题 C ++编程风格 ,我有兴趣了解有什么想法。



我看到了一个相关问题, 为什么C ++编译需要这么长时间? ,但是不提供许多解决方案。






这里有Visual Studio支持在项目之间共享预编译头

解决方案

语言技巧



Pimpl Idiom



查看 Pimpl成语 此处此处(也称为< a href =http://en.wikipedia.org/wiki/Opaque_pointer>不透明指针或句柄类。它不仅加快了编译速度,而且与无丢弃交换函数。



转发声明












b

只要有可能,请使用转发声明。如果编译器只需要知道 SomeIdentifier 是一个结构或指针或其他,不包括整个定义,强制编译器做比它需要的更多的工作。这可以有级联效应,使得这种方式比他们需要的慢。



I / O 流对于减缓构建是特别公知的。如果您在头文件中需要它们,请尝试#including < iosfwd> 而不是< iostream> 请仅在实现文件中包含< iostream> 头。 < iosfwd> 头仅包含转发声明。不幸的是,其他标准头没有相应的声明头。



希望通过引用传递给函数签名中的值。这将消除在头文件中包含相应类型定义的需要,并且您只需要转发声明类型。当然,更喜欢const引用非const引用以避免模糊的bug,但这是另一个问题的问题。



保护条件



使用保护条件使头文件不会在单个翻译单元中被多次包含。

  #pragma once 
#ifndef filename_h
#define filename_h

//标题声明/定义

#endif

通过使用pragma和ifndef,您可以获得纯宏解决方案的可移植性,以及一些编译器可以在 pragma once 指令的存在下执行。



减少相互依赖性



你的代码设计越模块化,相互依赖越少,你就越不需要重新编译一切。你也可以减少编译器在任何单独的块上同时做的工作量,因为它有较少的跟踪。



编译器选项



预编译头



这些用于编译包含头部的公共部分一次许多翻译单位。编译器编译一次,并保存其内部状态。



请注意,您只需要在预编译的文件中包含很少更改的内容,就可以快速加载该状态,从而使用同一组头文件编译另一个文件。标题,或者你可能最终做的完全重建更频繁的必要。这是 STL 标题和其他库包含文件的好地方。



ccache 是另一个利用缓存技术加快速度的实用程序。



使用并行性



许多编译器/ IDE支持使用多个内核/ CPU同时进行编译。在 GNU Make (通常与GCC配合使用)中,使用 -j [N] 选项。在Visual Studio中,在首选项下有一个选项,允许它并行构建多个项目。您还可以使用 / MP 选项

其他并行实用程序:





使用较低的优化级别





共享库



移动你的频率更低修改代码到库中可以减少编译时间。通过使用共享库( .so .dll ),您还可以减少链接时间。

获得更快的计算机



更多内存,更快的硬盘驱动器(包括SSD)和更多的CPU /编译速度差异。


What techniques can be used to speed up C++ compilation times?

This question came up in some comments to Stack Overflow question C++ programming style, and I'm interested to hear what ideas there are.

I've seen a related question, Why does C++ compilation take so long?, but that doesn't provide many solutions.


Vote here have Visual Studio support sharing precompiled headers between projects

解决方案

Language techniques

Pimpl Idiom

Take a look at the Pimpl idiom here, and here, also known as an opaque pointer or handle classes. Not only does it speed up compilation, it also increases exception safety when combined with a non-throwing swap function. The Pimpl idiom lets you reduce the dependencies between headers and reduces the amount of recompilation that needs to be done.

Forward Declarations

Wherever possible, use forward declarations. If the compiler only needs to know that SomeIdentifier is a struct or a pointer or whatever, don't include the entire definition, forcing the compiler to do more work than it needs to. This can have a cascading effect, making this way slower than they need to be.

The I/O streams are particularly known for slowing down builds. If you need them in a header file, try #including <iosfwd> instead of <iostream> and #include the <iostream> header in the implementation file only. The <iosfwd> header holds forward declarations only. Unfortunately the other standard headers don't have a respective declarations header.

Prefer pass-by-reference to pass-by-value in function signatures. This will eliminate the need to #include the respective type definitions in the header file and you will only need to forward-declare the type. Of course, prefer const references to non-const references to avoid obscure bugs, but this is an issue for another question.

Guard Conditions

Use guard conditions to keep header files from being included more than once in a single translation unit.

#pragma once
#ifndef filename_h
#define filename_h

// Header declarations / definitions

#endif

By using both the pragma and the ifndef, you get the portability of the plain macro solution, as well as the compilation speed optimization that some compilers can do in the presence of the pragma once directive.

Reduce interdependency

The more modular and less interdependent your code design is in general, the less often you will have to recompile everything. You can also end up reducing the amount of work the compiler has to do on any individual block at the same time, by virtue of the fact that it has less to keep track of.

Compiler options

Precompiled Headers

These are used to compile a common section of included headers once for many translation units. The compiler compiles it once, and saves its internal state. That state can then be loaded quickly to get a head start in compiling another file with that same set of headers.

Be careful that you only include rarely changed stuff in the precompiled headers, or you could end up doing full rebuilds more often than necessary. This is a good place for STL headers and other library include files.

ccache is another utility that takes advantage of caching techniques to speed things up.

Use Parallelism

Many compilers / IDEs support using multiple cores/CPUs to do compilation simultaneously. In GNU Make (usually used with GCC), use the -j [N] option. In Visual Studio, there's an option under preferences to allow it to build multiple projects in parallel. You can also use the /MP option for file-level paralellism, instead of just project-level paralellism.

Other parallel utilities:

Use a Lower Optimization Level

The more the compiler tries to optimize, the harder it has to work.

Shared Libraries

Moving your less frequently modified code into libraries can reduce compile time. By using shared libraries (.so or .dll), you can reduce linking time as well.

Get a Faster Computer

More RAM, faster hard drives (including SSDs), and more CPUs/cores will all make a difference in compilation speed.

这篇关于什么技术可以用来加快C ++编译时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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