提升编译速度

提升vc的链接速度在庞大的工程是很有用的。 以下要点: 1.设计的时候要注意抽象层次,比如一个对象不应该知道太多的其他对象,如果这里面很复杂混乱,那就应该分解他们,或者使用另外的机制,类似事件传递来达到类似的目的。 2.把工程分解成dll,这样能显著提高链接速度。 3.使用预编译头文件,把不常修改的文件放到预编译头文件中显然是很有用的,但是经常改变的就算了,否则更慢.... 4.使用增量链接选项。 5.项目间依赖项的设置也会大大降低链接速度,可以多使用这样的手段:#pragma comment(lib, "pathToLib"). 6.link time code generationg ,/LTCG,这个选项在debug时一定关掉。 7.快的硬盘,或memory,显然,这是有用的。 8.使用联合编译器。 以下是一篇老外的讲的非常好的文章: Language techniques Pimpl Idiom Take a look at the pimpl idiom here , and here . Also known as an Opaque Pointer or Handle. 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 IO streams are particularly known for slowing down builds. If you need them in a header file, try #including instead of and #include the header in the implementation file only. The 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 interdependancy The more modular and less interdependant 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 of 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 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: Incredibuild Unity Build distcc 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, more CPUs/cores will all make a difference in compilation speed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值