Speed up GCC Compile time

Правка en1, от _Na2Th, 2017-08-17 05:05:24

This is a tip for C++ programmers who use the bits header to include every library from C++.

#include <bits/stdc++.h>

Even more so with C++11, it may take some time for it to compile on your machine. I speak by experience: it took me more than 4s to compile every single time. This would drive me crazy during contests.

I know it works on linux with gcc. I would guess it works on any unix with gcc, but I really don't know.

The solution I offer here is to precompile the headers, and avoid processing every header file every time.

You first need to find where the header is. It is possible to do so by compiling a program which includes the librarty with the -H flag. Let a.cpp be

#include <bits/stdc++.h>
using namespace std;
int main(){
    cout << "It could be any program here, really\n";
}

and run

g++ -H a.cpp

The first file is the header we are looking for. In my machine it was on /usr/include/c++/7.1.1/x86_64-pc-linux-gnu/bits.

So I can just copy this file into the directory I am going to use during the competition and precompile it. I do it in a way that I do not have to change the header, so that I can submit the file normally.

To be able to do so, create a bits directory, copy the file into it, and compile the header with the same flags you use. In the code below, remember to change the directory to the one you found with the g++ -H command.

mkdir bits
cp /usr/include/c++/7.1.1/x86_64-pc-linux-gnu/bits/stdc++.h bits/
cd bits
g++ -std=c++11 stdc++.h

This precompiles the header, and then I just need to change my templates to use ~~~~

include "bits/stdc++.h"

~~~~ so that in my local machine it uses the compiled version, but still works on the online judge.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский _Na2Th 2017-08-17 05:30:41 69 (published)
en3 Английский _Na2Th 2017-08-17 05:28:55 575
en2 Английский _Na2Th 2017-08-17 05:22:09 1027
en1 Английский _Na2Th 2017-08-17 05:05:24 1868 Initial revision (saved to drafts)