如何加快g ++编译时间(当使用很多模板时) [英] How to speed up g++ compile time (when using a lot of templates)

查看:101
本文介绍了如何加快g ++编译时间(当使用很多模板时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能有点奇怪,但我如何加快g ++编译时间?我的C ++代码大量使用boost和模板。我已经尽可能多地移出头文件并使用-j选项,但仍然需要一段时间来编译(和链接)。

This question is perhaps somehow odd, but how can I speed up g++ compile time? My C++ code heavily uses boost and templates. I already moved as much as possible out of the headers files and use the -j option, but still it takes quite a while to compile (and link).

有任何工具,在那里分析我的代码,指出编译器瓶颈?或者可以以某种方式剖析我的代码运行的编译器?这将是非常好的,因为有时我有一个印象,我花了太多时间盯着编译器控制台日志...

Are there any tools out there which analyse my code and point out bottle-necks for the compiler? Or can one somehow profile the compiler running on my code? This would be really nice, because sometimes I have the impression, that I spent too much time staring at the compiler console log ...

推荐答案

对我最有用的是:


  • 基于RAM文件系统。这在Linux上是微不足道的。您可能希望在RAM文件系统上保留公共头文件(预编译或实际的.h文件)的副本。

  • 预编译头。我有一个每(主要)库(例如Boost,Qt,stdlib)。

  • 在可能的情况下声明,而不是包含类。这会减少依赖关系,从而减少更改头文件时需要重新编译的文件数。

  • 并行化。这通常有助于在个案基础上,但我有 -j3 全局为make。

  • 使用 -O0 ,如果您的Makefile中的依赖关系图是正确的,没有测试执行速度或代码大小(和您的计算机是足够快,你不关心很多(可能是小的)性能打击)。

  • 每次保存时编译。有些人不喜欢这个,但它允许你早期看到错误,并可以在后台完成,减少你必须等待,当你完成写作并准备测试的时间。

  • Build on a RAM filesystem. This is trivial on Linux. You may want to keep a copy of common header files (precompiled or the actual .h files) on the RAM filesystem as well.
  • Precompiled headers. I have one per (major) library (e.g. Boost, Qt, stdlib).
  • Declare instead of include classes where possible. This reduces dependencies, thus reduces the number of files which need to be recompiled when you change a header file.
  • Parallelize make. This usually helps on a case-by-case basis, but I have -j3 globally for make. Make sure your dependency graphs are correct in your Makefile, though, or you may have problems.
  • Use -O0 if you're not testing execution speed or code size (and your computer is fast enough for you not to care much about the (probably small) performance hit).
  • Compile each time you save. Some people don't like this, but it allows you to see errors early and can be done in the background, reducing the time you have to wait when you're done writing and ready to test.

这篇关于如何加快g ++编译时间(当使用很多模板时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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