Well, the following code works well for quick output using putchar_unlocked () .
#define pc(x) putchar_unlocked(x); inline void writeInt (int n) { int N = n, rev, count = 0; rev = N; if (N == 0) { pc('0'); pc('\n'); return ;} while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s rev = 0; while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;} //store reverse of N in rev while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;} while (count--) pc('0'); }
Printf usually prints pretty quickly, but for Integer or Long Outputs, the function below is a bit slower.
Here we use the putchar_unlocked () method to output a character that looks like an unsafe version of putchar () and faster.
See link.
source share