patternjavaMinor
Reading input fast in Java for competitive programming
Viewed 0 times
fastreadingprogrammingjavainputforcompetitive
Problem
I have been solving problems on Timus Online Judge. In the beginning, I didn't worry much about I/O operations, so I just took some code from the internet and focused more on the logic. But after going through the helper class that I was using, I created my own class.
This is the original stuff:
This is what I have written now:
```
import java.io.IOException;
public class IOUtils {
public static String readString() throws Exception {
int c = System.in.read();
while (isSpaceChar(c))
c = System.in.read();
StringBuilder res = new StringBuilder();
do {
res.appendC
This is the original stuff:
import java.io.InputStream;
public 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() throws Exception {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() throws Exception {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() throws Exception {
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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}This is what I have written now:
```
import java.io.IOException;
public class IOUtils {
public static String readString() throws Exception {
int c = System.in.read();
while (isSpaceChar(c))
c = System.in.read();
StringBuilder res = new StringBuilder();
do {
res.appendC
Solution
In my experience, non-buffered I/O based on plain System.in and System.out are not fast enough for some competitive programming problems. I wrote a template based on BufferedReader and BufferedWriter, which are much faster. They aren't necessary for every problem, but I haven't found any downside to using them by default.
Some example code:
I wrote a couple of blog posts with more details and some benchmark numbers:
Some example code:
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in);
while (stdin.ready())
String line = stdin.readLine();
BufferedWriter stdout = new BufferedWriter(new OutputStreamWriter(System.out));
stdout.write("test");
stdout.flush();
stdout.close();I wrote a couple of blog posts with more details and some benchmark numbers:
- Solving UVa 11340 in Java
- Why is Java I/O Slow?
Code Snippets
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in);
while (stdin.ready())
String line = stdin.readLine();
BufferedWriter stdout = new BufferedWriter(new OutputStreamWriter(System.out));
stdout.write("test");
stdout.flush();
stdout.close();Context
StackExchange Code Review Q#121448, answer score: 2
Revisions (0)
No revisions yet.