现在的位置: 首页 > 综合 > 正文

提升vc编译,链接速度

2014年02月01日 ⁄ 综合 ⁄ 共 3351字 ⁄ 字号 评论关闭
文章目录

 提升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 <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 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:

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.

 

 

【上篇】
【下篇】

抱歉!评论已关闭.