_Na2Th's blog

By _Na2Th, history, 2 years ago, In English,

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.

I'll explain by example, showing how I would prepare for the upcoming contest, round 429.

The first thing to do is to make the folder I'll leave my code in, and copy the template file I have into it.

mkdir cf429
cp temp.cpp cf429/

The template goes something like

#include "bits/stdc++.h"
#define mt make_tuple
using namespace std;

int main(){

Note the use of #include "bits/stdc++.h" instead of #include <bits/stdc++.h>. This is important, since it makes the compiler first look for the local copy, and only afterwards try to look in the libraries. This is what ensures that the code will run on the Online Judge and take advantage of the precompiled local version in my machine.

Then I would need to find where the header is. It is possible to do so by compiling a program which includes the library with the -H flag. I'll just compile my template.

g++ -H temp.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

Now, just to ensure that I'll will have the same flags, I use a Makefile.

cd ..
echo "CXXFLAGS = --std=c++11" > Makefile

and done! I can just wait for the contest to begin, and compile my programs much faster with a simple make A.

Note that you can freely modify the local header copy, removing some stuff that you deem useless, to make it compile even faster. Also, you can then just copy this local version into the directories used in the competition. With this special local version, it is quite easy even to make a script that does the set up mentioned above.

  • Vote: I like it
  • +52
  • Vote: I do not like it

2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by _Na2Th (previous revision, new revision, compare).

2 years ago, # |
Rev. 2   Vote: I like it +27 Vote: I do not like it

That really makes a difference. It should work with gcc (regardless the OS). In my case it reduced the compilation time by a factor of 6.

You could also precompile the actual header

g++ -std=c++11 /usr/include/long_path_to_your_libraries/bits/stdc++.h

and it would work for every directory on your system. Personally I think this is more convenient, although less customizable.

  • »
    2 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    Yeah, on my machine it got 6 times faster too, but by cherrypicking the headers in the local version I got the compilation time down to ~.3s, which is 12 times faster.

    Aside from not being advisable to change the global header, you would need sudo access to compile, which is not the case in competitions.

    Some modification of this procedure can be used as a preparation on a ACM-ICPC style competition, even if you have no sudo privileges.

    • »
      2 years ago, # ^ |
      Rev. 2   Vote: I like it +10 Vote: I do not like it

      You are not changing the global header, just precompiling it to spare reprocessing it all the time (which involves a lot of conditional expansions). GCC is smart enough to use the precompiled header only when possible (same set of flags, same version, etc). If you really want to change the header, then it's better to create a copy but just to precompile it is already a nice optimization.

      About the permission issue, in my case it didn't require any special permissions. The only issue I can think of is that you can't create a file inside the long_path/bits directory (which you need because GCC creates a .gch file). I always get confused with directory permissions, so I can't say what permissions you actually need.

12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I wrote a small script to do the same.