针对编译时间进行优化的 STL 版本?

发布于 2024-08-29 10:50:48 字数 140 浏览 2 评论 0原文

我正在寻找一种 STL 的变体(如果它不具备所有功能也没关系),该变体针对较短的编译时间进行了优化 - 我对较长的编译时间感到困扰,这会延迟我的编译 -调试-编辑循环。

我主要对 STL 的容器感兴趣:矢量/地图,而不是算法。

谢谢!

I'm looking for a variant of the STL (it's okay if it doesn't have all the functionality) that's optimized for short compile times -- I get bothered by long compile times that delay my compile-debug-edit cycle.

I'm mainly interested in the containers of the STL: vector/map, and not so much the algorithms.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小帐篷 2024-09-05 10:50:49

由于您只对 STL 的一部分感兴趣,因此您是否考虑过对头文件进行分支,以便不包含未使用的内容。

Since you're only interested in part of the STL, have you considered branching the header files so that the unused stuff is not included.

夏日浅笑〃 2024-09-05 10:50:49

买一台更快的电脑? :) 说真的,我们发现模板确实会消耗链接时间。为了解决这个问题,我们只将当前正在调试的模块保留为完全调试模式(-o0)。我们将其他模块编译为高于 0 的优化级别,不会生成几乎那么多的死代码供链接器解析。

Buy a faster computer? :) Seriously though, we've found that templates really eat up link times. To get around this, we only leave the modules we are currently debugging in full debug mode (-o0). The other modules we compile to some optimization level above 0 that doesn't generate nearly as much dead code for the linker to resolve.

指尖上的星空 2024-09-05 10:50:48

查看编译器的预编译头选项。 在 GCC 中,例如,传递标头,就好像它是源一样导致它被预编译。

它显着减少了我的小测试的时间,但前提是您不计算预编译所花费的时间:

Shadow:code dkrauss$ ls maps*
maps.cpp    maps.h      maps2.cpp
Shadow:code dkrauss$ cat maps*
#include "maps.h"
using namespace std;

map<int,int> ints;
map<string, string> strings;
map<int, string> is;
map<string, int> si;

int main() {
bang(ints);
bang(strings);
bang(is);
bang(si);

extern void more();
more();
}
#include <string>
#include <map>

template< class K, class V >
void bang( std::map<K,V> &v ) {
v[ K() ] = V();
v.erase( v.begin() );
}

#include "maps.h"
using namespace std;

map<int,int> ints2;
map<string, string> strings2;
map<int, string> is2;
map<string, int> si2;

void more() {
bang(ints2);
bang(strings2);
bang(is2);
bang(si2);
}
Shadow:code dkrauss$ time g++ maps*.cpp -o maps

real    0m1.091s
user    0m0.857s
sys 0m0.132s
Shadow:code dkrauss$ time g++ maps.h

real    0m0.952s
user    0m0.406s
sys 0m0.110s
Shadow:code dkrauss$ ls maps*
maps        maps.cpp    maps.h      maps.h.gch  maps2.cpp
Shadow:code dkrauss$ time g++ maps*.cpp -o maps

real    0m0.718s
user    0m0.552s
sys 0m0.095s
Shadow:code dkrauss$ 

Take a look at your compiler's options for precompiled headers. In GCC, for example, passing a header as if it is a source causes it to be precompiled.

It reduced time significantly for my little test, but only if you don't count the time spent precompiling:

Shadow:code dkrauss$ ls maps*
maps.cpp    maps.h      maps2.cpp
Shadow:code dkrauss$ cat maps*
#include "maps.h"
using namespace std;

map<int,int> ints;
map<string, string> strings;
map<int, string> is;
map<string, int> si;

int main() {
bang(ints);
bang(strings);
bang(is);
bang(si);

extern void more();
more();
}
#include <string>
#include <map>

template< class K, class V >
void bang( std::map<K,V> &v ) {
v[ K() ] = V();
v.erase( v.begin() );
}

#include "maps.h"
using namespace std;

map<int,int> ints2;
map<string, string> strings2;
map<int, string> is2;
map<string, int> si2;

void more() {
bang(ints2);
bang(strings2);
bang(is2);
bang(si2);
}
Shadow:code dkrauss$ time g++ maps*.cpp -o maps

real    0m1.091s
user    0m0.857s
sys 0m0.132s
Shadow:code dkrauss$ time g++ maps.h

real    0m0.952s
user    0m0.406s
sys 0m0.110s
Shadow:code dkrauss$ ls maps*
maps        maps.cpp    maps.h      maps.h.gch  maps2.cpp
Shadow:code dkrauss$ time g++ maps*.cpp -o maps

real    0m0.718s
user    0m0.552s
sys 0m0.095s
Shadow:code dkrauss$ 
ま柒月 2024-09-05 10:50:48

由于 STL 严重依赖模板,因此真正让您感兴趣的是编译器处理基于模板的代码的速度。 GCC 在每个新版本中都在提高编译模板代码的速度,因此您可能需要考虑更新编译器。但除此之外,您无法做太多事情来加速基于模板的代码。其他一些编译标志可以提高构建速度;例如,省略优化标志将使其编译速度更快,而使用预编译头也可以大大加快编译速度。如果您有一台具有多个内核的计算机,则许多构建系统都支持并行编译(例如,make 使用“-j”标志(如“-j 2”中所示)来指定有多少个内核)。我还应该指出,您只需为您使用的内容付费;如果您包含;和<地图>但不是,那么您根本不需要为其付费...因此,如果您包含它但不使用它,那么请停止包含它。

Since STL relies heavily on templates, what will really get you is how fast the compiler is capable of processing template-based code. GCC has been improving its speed at compiling template code with each new release, so you might want to consider updating your compiler. Short of that, though, there isn't much you can do to speed up template-based code. Some other compilation flags can improve the build speed; for example, omitting the optimization flag will make it compile faster, and using precompiled headers can also greatly speed up compilation. If you have a computer with multiple cores, many build systems support compilation in parallel (for example, make uses the "-j" flag as in "-j 2" to specify how many cores). I should also point out that you only pay for what you use; if you include <vector> and <map> but not <algorithm>, then you won't be paying for it at all... so if you are including it but not using it, then stop including it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文