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.

mtk03:33 PM cpp/ > time g++ -DCODING -std=c++11 -Wall -g template.cpp
real 0m6.993s
user 0m6.428s
sys 0m0.424s
# Compiling bits/stdc++.h and storing
mtk03:34 PM cpp/ > time sudo g++ -DCODING -std=c++11 -Wall -g /usr/include/i386-linux-gnu/c++/5/bits/stdc++.h
real 0m8.408s
user 0m7.748s
sys 0m0.544s
mtk03:34 PM cpp/ > time g++ -DCODING -std=c++11 -Wall -g template.cpp
real 0m2.442s
user 0m2.180s
sys 0m0.212s
/* ________________________________
/\ \
\_| Bismillah-ir-Rahman-ir-Rahim |
| ___________________________|_
\_/_____________________________/
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define de(x) cerr << #x << "=" << x << endl
#define p(x) cerr << x << endl // print
#define PI acos(-1.0)
#define all(c) (c).begin(),(c).end()
#ifdef CODING
template<typename I> void _DOING(const char *s,I&& x){cerr<<s<<"="<<x<<endl;}//without ','
template<typename I,typename... T> void _DOING(const char *s,I&& x,T&&... tail) {//with ','
int c=0;
static const char bra[]="({[", ket[]=")}]";
while(*s!=',' || c!=0) { //eg. mkp(a,b)
if(strchr(bra,*s)) c++;
if(strchr(ket,*s)) c--;
cerr<<*s++;
}
cerr<<"="<<x<<", ";
_DOING(s+1,tail...);
}
#define debug(...) do{\
fprintf(stderr,"%s:%d - ",__PRETTY_FUNCTION__,__LINE__);\
_DOING(#__VA_ARGS__,__VA_ARGS__);\
}while(0);
template<typename It> ostream& _OUTC(ostream &s,It b,It e, string st, string end) {
s<<st; for(auto it=b;it!=e;it++) s<<(it==b?"":" ")<<*it; s<<end; return s;
}
template<typename A,typename B> ostream& operator <<(ostream&s, const pair<A,B> &p){return s<<"("<<p.first<<","<<p.second<<")";}
template<typename A,typename B> ostream& operator <<(ostream&s, const map<A,B> &c){return _OUTC(s,all(c),"{{ ", " }}");}
template<typename T> ostream& operator <<(ostream&s, const set<T> &c){return _OUTC(s,all(c),"{ ", " }");}
template<typename T> ostream& operator <<(ostream&s, const vector<T> &c){return _OUTC(s,all(c),"[", "]");}
#else
#define debug(...)
#endif
typedef long long ll;
int isPrime(ll x){ll s= sqrt(x);int i;if(x<=1) return 0;if(x==2 || x==3) return 1;if(x%2==0|| x%3==0) return 0; for(i=5;i<=s;i+=6) if(x%i==0||x%(i+2)==0) return 0; return 1;}
void log(string s) { cout << s <<endl; }
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s); string item;
while (getline(ss, item, delim)) if(!item.empty()) elems.push_back(item);
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems; split(s, delim, elems); return elems;
}
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define present(c,x) ((c).find(x) != (c).end()) // logn
#define vpresent(c,x) (find(all(c),x) != (c).end()) // o(n)
ll mod = 1e9 + 7;
int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(NULL);
ll t;
set<int> a = {5,6,7,8};
vector< double > d = {4,6,9,2};
map<int, int> m ;
m[2] = 4;
m[22] = 14;
m[12] = 454;
cout<< a << '\n';
cout<< d << '\n';
cout<< m << '\n';
return 0;
}
/* Sample output
{ 5 6 7 8 }
[4 6 9 2]
{{ (2,4) (12,454) (22,14) }}
*/
view raw template.cpp hosted with ❤ by GitHub
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