Sunday, August 28, 2016

Quick Compilation of bits/stdc++.h header for competitive coding

If you are using bits/stdc++.h header to include all the header files at once, then probably you are also facing compilation time of about 8 to 10 seconds. This compilation time is long during contest, as you need to get the output fast to go with the Read-Eval-Print-Loop( REPL ) approach, and it can be frustrating at times.
There is very easy way to reduce this compilation time, without any external tools like cache, or make command tricks.

You just need to pre-compile the bits/stdc++.h file. Run the below command once, and g++ will use the pre-compiled header whenever you invoke g++ to compile cpp files.

You may add any compiler option you may want to. e.g. compiling with -g flag produces a larger object file.
sudo  g++ -std=c++11 -Wall  /usr/include/i386-linux-gnu/c++/5/bits/stdc++.h
Output of above command on my ubuntu 15.10 (32 bit) machine is

$ ls -lrth /usr/include/i386-linux-gnu/c++/5/bits/stdc++.h.gch
-rw-r--r-- 1 root root 46M Aug 28 15:57 /usr/include/i386-linux-gnu/c++/5/bits/stdc++.h.gch

Here is my sample template.cpp file, that uses this header along with some macro that helps in debugging variables, vectors, maps, sets easily in c++. The txt files are numbered in order they were executed.

Some more useful info in the comments to this blog entry on codeforces.

Please subscribe to the blog, share the post if you like it. Leave a comment about mistakes in blog post, problems you would like to solve, get help on. Let us know if you have any other feedback about the blog.


No comments:

Post a Comment