views:

130

answers:

4

Hi!

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!

A: 

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.

Preet Sangha
Nothing unused should be included anyway.
Potatoswatter
That's at link time, not compile time.
Preet Sangha
Why would the library implementer `#include` an unused header? Conversely, if you strip any headers, you're likely to cause an unknown identifier error, whether you call the function causing the error or not.
Potatoswatter
+4  A: 

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$ 
Potatoswatter
A: 

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.

Michael Dorgan
do you mean 'buy'?
Tom
Lol! that too! Go super fast type and send without proof reading!
Michael Dorgan
Maybe he was referring to http://en.wikipedia.org/wiki/Intel_C%2B%2B_Compiler
Nick Presta
+1  A: 

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.

Michael Aaron Safyan