Using putchar_unlocked for quick output

I want to use fast input and output in my code. I figured out using getchar_unlocked for quick input using the following function.

 inline int next_int() { int n = 0; char c = getchar_unlocked(); while (!('0' <= c && c <= '9')) { c = getchar_unlocked(); } while ('0' <= c && c <= '9') { n = n * 10 + c - '0'; c = getchar_unlocked(); } return n; } 

can someone explain me how to use quick output using putchar_unlocked() function?

I went through this question , and there someone said that putchar_unlocked() can be used for quick output.

+6
source share
1 answer

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.

+7
source

Source: https://habr.com/ru/post/950861/


All Articles