开发者

Version of STL optimized for compile time?

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!


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$ 


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜