can someone explain me what’s fast input/output. I m sorta new to this stuff and would really appreciate some help. Thank you.
If you submission passed TEST problem, you used fast I/O methods
Simply it means not to use cin and cout. Brief description why to use it is here.
1: if you are using C then you can use following function for fast IO:
inline void Scan_f(int *a) { char c = 0; while(c<33) //c = fgetc_unlocked(stdin); c = getc(stdin); *a = 0; while(c>33) { *a = *a*10 + c - '0'; //c = fgetc_unlocked(stdin); c = getc(stdin); }
and in place of scanf(“%d”,&t) you should use this function as Scan_f(&t) only.
2: if you are using C++ you can use following technique used by @mukel FAST IO for C++ which is very good.
3:now in JAVA you can create a class which will read the input Fastly by following code
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class OutputWriter {
private PrintWriter writer;
public OutputWriter(OutputStream stream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(int x) {
writer.println(x);
}
public void print(int x) {
writer.print(x);
}
public void printSpace() {
writer.print(" ");
}
public void close() {
writer.close();
}
}
now you can use above two classes **InputReader** and **OutputWriter** for Input and output Respectively.use System.in as InputStream for constructor InputReader(System.in).
@omniscius
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.
you can use any one of these functions although fgets is fastest
#include
#include
#include
#include
std::chrono::time_pointstd::chrono::steady_clock hClock()
{
return std::chrono::steady_clock::now();
}
std::uint32_t TimeDuration(std::chrono::time_pointstd::chrono::steady_clock Time)
{
return std::chrono::duration_caststd::chrono::nanoseconds(hClock() - Time).count();
}
void Benchmark(const char* Name, std::string &str, void(*func)(std::string &str))
{
auto time = hClock();
for (int i = 0; i < 100; ++i)
{
func(str);
str.clear();
}
std::cout<<Name<<" took: “<<TimeDuration(time) / 100<<” nano-seconds.\n";
}
void unlocked_bench(std::string &str)
{
char c = ‘0’;
while((c = getchar_unlocked()) && (c != -1 && c != ‘\n’ && c != ‘\r’))
{
str += c;
}
}
void getchar_bench(std::string &str)
{
char c = ‘0’;
while((c = getchar()) && (c != -1 && c != ‘\n’ && c != ‘\r’))
{
str += c;
}
}
void getline_bench(std::string &str)
{
std::cin.getline(&str[0], str.size());
}
void scanf_bench(std::string &str)
{
scanf("%[^\n]100s", &str[0]);
}
void fgets_bench(std::string &str)
{
fgets(&str[0], str.size(), stdin);
}
void cinread_bench(std::string &str)
{
std::cin.read(&str[0], str.size());
}
int main()
{
std::string str;
str.reserve(100);
Benchmark("getchar_unlocked", str, unlocked_bench);
Benchmark("getchar", str, getchar_bench);
Benchmark("getline", str, getline_bench);
Benchmark("scanf", str, scanf_bench);
Benchmark("fgets", str, fgets_bench);
Benchmark("cinread", str, cinread_bench);
return 0;
}
inline void Scan_f(int *a)
{
char c = 0;
*a=0;
while((c=getchar())!=’\n’)
*a=(*a)*10+c-‘0’;
}
is this function fine to go on?? or i should make some changes.Please answer with explanation.
i have used (*a) not (a).My post has somehow got manipulated automaticaly.
betlista, its not the TEST problem, it is the INTEST that uses fast input/output methods. One could find the fast input/output for different languages by clicking the ALL SUBMISSIONS button. #HappyCoding
Your code goes here:
inline void Scan_f(int b)
{
char n = 0;
while(n<33)
n = getc(stdin);
b = 0;
while(c>33)
{
b = b*10 + n - ‘0’;
n = getc(stdin);
}
happy coding
When I am using fast I/O code in java for a program, I m getting WA where as the same code is giving TLE+AC without fast I/O. Can somebody tell me what all can be the possible reasons for this conditon? Thanks in advance…
if you can give your code, we may help you better. Though I suspect it has something like string input taking null character at end of line, or something related to incompatible data types/out of range values or input not assigning them values properly (due to which garbage values are being used- Like if we forget’&’ in scanf("%d",&n); )
inline void Scan_f(int b) { char n = 0; while(n<33) n = getc(stdin); b = 0; while(c>33) { b = b*10 + n - ‘0’; n = getc(stdin); }
in this code c is not difine…
The above given C code doesn’t work with the given format Scan_f(&n) because int a expects an integer value. You should properly update this.
can u please explain what does two character comparison means in your code i.e.
“while(c<‘0’ || c>‘9’)” implies .Are comparison r occuring in ASCII values or some other context
Can some1 explain…! how does it work?? how is it fast ??
checking for char is integer or not…
this means…c should belong {‘0’,‘1’,‘2’…‘9’}
we are doing this to avoid any unwanted character or junk data…
hope you get it
I am happy to help dear
The above method which you said in C for faster input isn’t working… again also I am getting 0.01 sec extra…