can anyone suggest which is the fastest way of inputting in c++.
getchar_unlocked is very fast and easy to code ( nearly the fastest ), you can check its performance on INTEST question. Using it, you can make custom reading functions for strings, integers and basically everything. Like for inputting non-negative integers you can do:
#define gc getchar_unlocked int read_int() { char c = gc(); while(c<'0' || c>'9') c = gc(); int ret = 0; while(c>='0' && c<='9') { ret = 10 * ret + c - 48; c = gc(); } return ret; }
On windows while testing , you can change #define gc getchar_unlocked to #define gc getchar and change back while submitting.
There is no any special library in C/C++ for FASTIO.
The Fastest Method to print output in C/C++ is fwrite or fwrite_unlocked in stdio.h library.
Also there isn’t any special/Direct function to print output at once .
Indirectly,it can be achieved using append function ( appending all output strings to one single string and printing the final string)in cplusplus and even strcat function in string.h lib,but performance wise ,“Appending or concat + printing”,is slower ,even far slower than printf.
So the fastest way in c/c++ is :directly read the stream in the raw form, and extract the information required.Also put the output in raw form in a huge buffer and display it finally using fwrite.
Please go through the link
@amitsaharana: There is a better way to change code between local testing and online submission-
#ifndef ONLINE_JUDGE
#define gc getchar
#else
#define gc getchar_unlocked
#endif
This works because ONLINE_JUDGE is a macro which is defined on judge, and not defined on your local machine.
This is extremely useful, as you don’t have to change code before submitting.
why in windows we can’t use getchar_unlocked()