Skip to content

Instantly share code, notes, and snippets.

@SamadiPour
Last active April 28, 2019 16:03
Show Gist options
  • Save SamadiPour/10b2cdb2d0ec630792f594f7dda1e3fd to your computer and use it in GitHub Desktop.
Save SamadiPour/10b2cdb2d0ec630792f594f7dda1e3fd to your computer and use it in GitHub Desktop.
Benchmark of different I/O in java
package TA_Class;
import java.io.*;
import java.time.Duration;
import java.time.Instant;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
public class InputTester {
private final static String FILE_PATH = "src/Numbers.txt";
private final static int TOTAL_RUN = 10;
private final static int TOTAL_NUMBER = 10_000_000;
private final static int NUMBER_IN_LINE = 20;
private final static int ITERATE = 5;
public static void main(String[] args) throws IOException {
Random random = new Random();
Instant startTime;
Instant endTime;
long duration;
long average;
int i = 0;
//----------------------------------Create File-------------------------
File file = new File(FILE_PATH);
if (file.exists()) {
file.delete();
file.createNewFile();
}
for (int j = 0; j < TOTAL_RUN; j++) {
int totalNumberTillNow = TOTAL_NUMBER * (j + 1) / TOTAL_RUN;
//----------------------------------Fill File-------------------------
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true));
StringBuilder stringBuilder;
for (int k = 0; k < TOTAL_NUMBER / (TOTAL_RUN * NUMBER_IN_LINE); k++) {
stringBuilder = new StringBuilder();
for (int l = 0; l < NUMBER_IN_LINE; l++) {
if (l == NUMBER_IN_LINE - 1)
stringBuilder.append(random.nextInt());
else
stringBuilder.append(random.nextInt()).append(" ");
}
writer.append(stringBuilder.toString());
writer.flush();
writer.newLine();
}
writer.close();
//----------------------------------Scanner-------------------------
average = 0;
for (int k = 0; k < ITERATE; k++) {
Scanner scanner = new Scanner(new FileInputStream(FILE_PATH));
startTime = Instant.now();
for (i = 0; i < totalNumberTillNow; i++) {
scanner.nextInt();
}
endTime = Instant.now();
duration = Duration.between(startTime, endTime).toMillis();
average += duration;
}
System.out.println("Scanner\t\t\t" + " With " + totalNumberTillNow + " Number Took " + average / ITERATE + "ms");
//----------------------------------UltraFastReader-------------------------
average = 0;
for (int k = 0; k < ITERATE; k++) {
UltraFastReader UltraFastReader = new UltraFastReader(FILE_PATH);
startTime = Instant.now();
for (i = 0; i < totalNumberTillNow; i++) {
UltraFastReader.nextInt();
}
endTime = Instant.now();
duration = Duration.between(startTime, endTime).toMillis();
average += duration;
}
System.out.println("UltraFastReader\t" + " With " + totalNumberTillNow + " Number Took " + (average / ITERATE) + "ms");
//----------------------------------InputReader-------------------------
average = 0;
for (int k = 0; k < ITERATE; k++) {
InputReader inputReader = new InputReader(new FileInputStream(FILE_PATH));
startTime = Instant.now();
for (i = 0; i < totalNumberTillNow; i++) {
inputReader.nextInt();
}
endTime = Instant.now();
duration = Duration.between(startTime, endTime).toMillis();
average += duration;
}
System.out.println("InputReader\t\t" + " With " + totalNumberTillNow + " Number Took " + average / ITERATE + "ms");
//----------------------------------BufferedReader-------------------------
average = 0;
for (int k = 0; k < ITERATE; k++) {
BufferedReader bufferedReader = new BufferedReader(new FileReader(FILE_PATH));
String text;
startTime = Instant.now();
for (i = 0; i < totalNumberTillNow / NUMBER_IN_LINE; i++) {
text = bufferedReader.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(text);
for (int f = 0; f < NUMBER_IN_LINE; f++) {
Integer.parseInt(stringTokenizer.nextToken());
}
}
endTime = Instant.now();
duration = Duration.between(startTime, endTime).toMillis();
average += duration;
}
System.out.println("BufferReader\t" + " With " + totalNumberTillNow + " Number Took " + average / ITERATE + "ms");
//----------------------------------FastReader-------------------------
average = 0;
for (int k = 0; k < ITERATE; k++) {
FastReader fastReader = new FastReader(FILE_PATH);
startTime = Instant.now();
for (i = 0; i < totalNumberTillNow; i++) {
fastReader.scanInt();
}
endTime = Instant.now();
duration = Duration.between(startTime, endTime).toMillis();
average += duration;
}
System.out.println("FastReader\t\t" + " With " + totalNumberTillNow + " Number Took " + average / ITERATE + "ms");
System.out.println("------------------------------------------------------");
}
}
}
//----------------------------------InputReader-------------------------
class InputReader {
private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
private static final InputStream DEFAULT_STREAM = System.in;
private static final int MAX_DECIMAL_PRECISION = 21;
private int c;
private byte[] buf;
private int bufferSize, bufIndex, numBytesRead;
private InputStream stream;
private static final byte EOF = -1;
private static final byte NEW_LINE = 10;
private static final byte SPACE = 32;
private static final byte DASH = 45;
private static final byte DOT = 46;
private char[] charBuffer;
private static byte[] bytes = new byte[58];
private static int[] ints = new int[58];
private static char[] chars = new char[128];
static {
char ch = ' ';
int value = 0;
byte _byte = 0;
for (int i = 48; i < 58; i++) bytes[i] = _byte++;
for (int i = 48; i < 58; i++) ints[i] = value++;
for (int i = 32; i < 128; i++) chars[i] = ch++;
}
// Primitive double lookup table used for optimizations.
private static final double[][] doubles = {
{0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d},
{0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d},
{0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d},
{0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d},
{0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d},
{0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d},
{0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d},
{0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d},
{0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d},
{0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d}
};
/**
* Create an InputReader that reads from standard input.
*/
public InputReader() {
this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);
}
/**
* Create an InputReader that reads from standard input.
*
* @param bufferSize The buffer size for this input reader.
*/
public InputReader(int bufferSize) {
this(DEFAULT_STREAM, bufferSize);
}
/**
* Create an InputReader that reads from standard input.
*
* @param stream Takes an InputStream as a parameter to read from.
*/
public InputReader(InputStream stream) {
this(stream, DEFAULT_BUFFER_SIZE);
}
/**
* Create an InputReader that reads from standard input.
*
* @param stream Takes an {@link java.io.InputStream#InputStream() InputStream} as a parameter to read from.
* @param bufferSize The size of the buffer to use.
*/
public InputReader(InputStream stream, int bufferSize) {
if (stream == null || bufferSize <= 0)
throw new IllegalArgumentException();
buf = new byte[bufferSize];
charBuffer = new char[128];
this.bufferSize = bufferSize;
this.stream = stream;
}
/**
* Reads a single character from the input stream.
*
* @return Returns the byte value of the next character in the buffer and EOF
* at the end of the stream.
* @throws IOException throws exception if there is no more data to read
*/
private byte read() throws IOException {
if (numBytesRead == EOF) throw new IOException();
if (bufIndex >= numBytesRead) {
bufIndex = 0;
numBytesRead = stream.read(buf);
if (numBytesRead == EOF)
return EOF;
}
return buf[bufIndex++];
}
/**
* Read values from the input stream until you reach a character with a
* higher ASCII value than 'token'.
*
* @param token The token is a value which we use to stop reading junk out of
* the stream.
* @return Returns 0 if a value greater than the token was reached or -1 if
* the end of the stream was reached.
* @throws IOException Throws exception at end of stream.
*/
private int readJunk(int token) throws IOException {
if (numBytesRead == EOF) return EOF;
// Seek to the first valid position index
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > token) return 0;
bufIndex++;
}
// reload buffer
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) return EOF;
bufIndex = 0;
} while (true);
}
/**
* Reads a single byte from the input stream.
*
* @return The next byte in the input stream
* @throws IOException Throws exception at end of stream.
*/
public byte nextByte() throws IOException {
return (byte) nextInt();
}
/**
* Reads a 32 bit signed integer from input stream.
*
* @return The next integer value in the stream.
* @throws IOException Throws exception at end of stream.
*/
public int nextInt() throws IOException {
if (readJunk(DASH - 1) == EOF) throw new IOException();
int sgn = 1, res = 0;
c = buf[bufIndex];
if (c == DASH) {
sgn = -1;
bufIndex++;
}
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > SPACE) {
res = (res << 3) + (res << 1);
res += ints[buf[bufIndex++]];
} else {
bufIndex++;
return res * sgn;
}
}
// Reload buffer
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) return res * sgn;
bufIndex = 0;
} while (true);
}
/**
* Reads a 64 bit signed long from input stream.
*
* @return The next long value in the stream.
* @throws IOException Throws exception at end of stream.
*/
public long nextLong() throws IOException {
if (readJunk(DASH - 1) == EOF) throw new IOException();
int sgn = 1;
long res = 0L;
c = buf[bufIndex];
if (c == DASH) {
sgn = -1;
bufIndex++;
}
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > SPACE) {
res = (res << 3) + (res << 1);
res += ints[buf[bufIndex++]];
} else {
bufIndex++;
return res * sgn;
}
}
// Reload buffer
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) return res * sgn;
bufIndex = 0;
} while (true);
}
/**
* Doubles the size of the internal char buffer for strings
*/
private void doubleCharBufferSize() {
char[] newBuffer = new char[charBuffer.length << 1];
for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];
charBuffer = newBuffer;
}
/**
* Reads a line from the input stream.
*
* @return Returns a line from the input stream in the form a String not
* including the new line character. Returns <code>null</code> when there are
* no more lines.
* @throws IOException Throws IOException when something terrible happens.
*/
public String nextLine() throws IOException {
try {
c = read();
} catch (IOException e) {
return null;
}
if (c == NEW_LINE) return ""; // Empty line
if (c == EOF) return null; // EOF
int i = 0;
charBuffer[i++] = (char) c;
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] != NEW_LINE) {
if (i == charBuffer.length) doubleCharBufferSize();
charBuffer[i++] = (char) buf[bufIndex++];
} else {
bufIndex++;
return new String(charBuffer, 0, i);
}
}
// Reload buffer
numBytesRead = stream.read(buf);
if (numBytesRead == EOF)
return new String(charBuffer, 0, i);
bufIndex = 0;
} while (true);
}
// Reads a string of characters from the input stream.
// The delimiter separating a string of characters is set to be:
// any ASCII value <= 32 meaning any spaces, new lines, EOF, tabs...
public String nextString() throws IOException {
if (numBytesRead == EOF) return null;
if (readJunk(SPACE) == EOF) return null;
for (int i = 0; ; ) {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > SPACE) {
if (i == charBuffer.length) doubleCharBufferSize();
charBuffer[i++] = (char) buf[bufIndex++];
} else {
bufIndex++;
return new String(charBuffer, 0, i);
}
}
// Reload buffer
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) return new String(charBuffer, 0, i);
bufIndex = 0;
}
}
// Returns an exact value a double value from the input stream.
public double nextDouble() throws IOException {
String doubleVal = nextString();
if (doubleVal == null) throw new IOException();
return Double.valueOf(doubleVal);
}
// Very quickly reads a double value from the input stream (~3x faster than nextDouble()). However,
// this method may provide a slightly less accurate reading than .nextDouble() if there are a lot
// of digits (~16+). In particular, it will only read double values with at most 21 digits after
// the decimal point and the reading my be as inaccurate as ~5*10^-16 from the true value.
public double nextDoubleFast() throws IOException {
c = read();
int sgn = 1;
while (c <= SPACE) c = read(); // while c is either: ' ', '\n', EOF
if (c == DASH) {
sgn = -1;
c = read();
}
double res = 0.0;
// while c is not: ' ', '\n', '.' or -1
while (c > DOT) {
res *= 10.0;
res += ints[c];
c = read();
}
if (c == DOT) {
int i = 0;
c = read();
// while c is digit and we are less than the maximum decimal precision
while (c > SPACE && i < MAX_DECIMAL_PRECISION) {
res += doubles[ints[c]][i++];
c = read();
}
}
return res * sgn;
}
// Read an array of n byte values
public byte[] nextByteArray(int n) throws IOException {
byte[] ar = new byte[n];
for (int i = 0; i < n; i++) ar[i] = nextByte();
return ar;
}
// Read an integer array of size n
public int[] nextIntArray(int n) throws IOException {
int[] ar = new int[n];
for (int i = 0; i < n; i++) ar[i] = nextInt();
return ar;
}
// Read a long array of size n
public long[] nextLongArray(int n) throws IOException {
long[] ar = new long[n];
for (int i = 0; i < n; i++) ar[i] = nextLong();
return ar;
}
// read an of doubles of size n
public double[] nextDoubleArray(int n) throws IOException {
double[] ar = new double[n];
for (int i = 0; i < n; i++) ar[i] = nextDouble();
return ar;
}
// Quickly read an array of doubles
public double[] nextDoubleArrayFast(int n) throws IOException {
double[] ar = new double[n];
for (int i = 0; i < n; i++) ar[i] = nextDoubleFast();
return ar;
}
// Read a string array of size n
public String[] nextStringArray(int n) throws IOException {
String[] ar = new String[n];
for (int i = 0; i < n; i++) {
String str = nextString();
if (str == null) throw new IOException();
ar[i] = str;
}
return ar;
}
// Read a 1-based byte array of size n+1
public byte[] nextByteArray1(int n) throws IOException {
byte[] ar = new byte[n + 1];
for (int i = 1; i <= n; i++) ar[i] = nextByte();
return ar;
}
// Read a 1-based integer array of size n+1
public int[] nextIntArray1(int n) throws IOException {
int[] ar = new int[n + 1];
for (int i = 1; i <= n; i++) ar[i] = nextInt();
return ar;
}
// Read a 1-based long array of size n+1
public long[] nextLongArray1(int n) throws IOException {
long[] ar = new long[n + 1];
for (int i = 1; i <= n; i++) ar[i] = nextLong();
return ar;
}
// Read a 1-based double array of size n+1
public double[] nextDoubleArray1(int n) throws IOException {
double[] ar = new double[n + 1];
for (int i = 1; i <= n; i++) ar[i] = nextDouble();
return ar;
}
// Quickly read a 1-based double array of size n+1
public double[] nextDoubleArrayFast1(int n) throws IOException {
double[] ar = new double[n + 1];
for (int i = 1; i <= n; i++) ar[i] = nextDoubleFast();
return ar;
}
// Read a 1-based string array of size n+1
public String[] nextStringArray1(int n) throws IOException {
String[] ar = new String[n + 1];
for (int i = 1; i <= n; i++) ar[i] = nextString();
return ar;
}
// Read a two dimensional matrix of bytes of size rows x cols
public byte[][] nextByteMatrix(int rows, int cols) throws IOException {
byte[][] matrix = new byte[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nextByte();
return matrix;
}
// Read a two dimensional matrix of ints of size rows x cols
public int[][] nextIntMatrix(int rows, int cols) throws IOException {
int[][] matrix = new int[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nextInt();
return matrix;
}
// Read a two dimensional matrix of longs of size rows x cols
public long[][] nextLongMatrix(int rows, int cols) throws IOException {
long[][] matrix = new long[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nextLong();
return matrix;
}
// Read a two dimensional matrix of doubles of size rows x cols
public double[][] nextDoubleMatrix(int rows, int cols) throws IOException {
double[][] matrix = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nextDouble();
return matrix;
}
// Quickly read a two dimensional matrix of doubles of size rows x cols
public double[][] nextDoubleMatrixFast(int rows, int cols) throws IOException {
double[][] matrix = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nextDoubleFast();
return matrix;
}
// Read a two dimensional matrix of Strings of size rows x cols
public String[][] nextStringMatrix(int rows, int cols) throws IOException {
String[][] matrix = new String[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nextString();
return matrix;
}
// Read a 1-based two dimensional matrix of bytes of size rows x cols
public byte[][] nextByteMatrix1(int rows, int cols) throws IOException {
byte[][] matrix = new byte[rows + 1][cols + 1];
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= cols; j++)
matrix[i][j] = nextByte();
return matrix;
}
// Read a 1-based two dimensional matrix of ints of size rows x cols
public int[][] nextIntMatrix1(int rows, int cols) throws IOException {
int[][] matrix = new int[rows + 1][cols + 1];
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= cols; j++)
matrix[i][j] = nextInt();
return matrix;
}
// Read a 1-based two dimensional matrix of longs of size rows x cols
public long[][] nextLongMatrix1(int rows, int cols) throws IOException {
long[][] matrix = new long[rows + 1][cols + 1];
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= cols; j++)
matrix[i][j] = nextLong();
return matrix;
}
// Read a 1-based two dimensional matrix of doubles of size rows x cols
public double[][] nextDoubleMatrix1(int rows, int cols) throws IOException {
double[][] matrix = new double[rows + 1][cols + 1];
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= cols; j++)
matrix[i][j] = nextDouble();
return matrix;
}
// Quickly read a 1-based two dimensional matrix of doubles of size rows x cols
public double[][] nextDoubleMatrixFast1(int rows, int cols) throws IOException {
double[][] matrix = new double[rows + 1][cols + 1];
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= cols; j++)
matrix[i][j] = nextDoubleFast();
return matrix;
}
// Read a 1-based two dimensional matrix of Strings of size rows x cols
public String[][] nextStringMatrix1(int rows, int cols) throws IOException {
String[][] matrix = new String[rows + 1][cols + 1];
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= cols; j++)
matrix[i][j] = nextString();
return matrix;
}
// Closes the input stream
public void close() throws IOException {
stream.close();
}
}
//----------------------------------FastReader-------------------------
class FastReader {
private byte[] buf = new byte[1024];
private int index;
private InputStream in;
private int total;
public FastReader() {
in = System.in;
}
public FastReader(String path) {
try {
in = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public int scanInt() throws IOException {
int integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
} else throw new InputMismatchException();
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
//----------------------------------UltraFastReader-------------------------
class UltraFastReader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public UltraFastReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public UltraFastReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
Scanner With 1000000 Number Took 844ms
UltraFastReader With 1000000 Number Took 37ms
InputReader With 1000000 Number Took 36ms
BufferReader With 1000000 Number Took 92ms
FastReader With 1000000 Number Took 83ms
------------------------------------------------------
Scanner With 2000000 Number Took 1497ms
UltraFastReader With 2000000 Number Took 82ms
InputReader With 2000000 Number Took 59ms
BufferReader With 2000000 Number Took 179ms
FastReader With 2000000 Number Took 148ms
------------------------------------------------------
Scanner With 3000000 Number Took 2157ms
UltraFastReader With 3000000 Number Took 121ms
InputReader With 3000000 Number Took 90ms
BufferReader With 3000000 Number Took 266ms
FastReader With 3000000 Number Took 221ms
------------------------------------------------------
Scanner With 4000000 Number Took 2857ms
UltraFastReader With 4000000 Number Took 162ms
InputReader With 4000000 Number Took 120ms
BufferReader With 4000000 Number Took 363ms
FastReader With 4000000 Number Took 285ms
------------------------------------------------------
Scanner With 5000000 Number Took 3520ms
UltraFastReader With 5000000 Number Took 190ms
InputReader With 5000000 Number Took 140ms
BufferReader With 5000000 Number Took 419ms
FastReader With 5000000 Number Took 341ms
------------------------------------------------------
Scanner With 6000000 Number Took 4252ms
UltraFastReader With 6000000 Number Took 237ms
InputReader With 6000000 Number Took 175ms
BufferReader With 6000000 Number Took 519ms
FastReader With 6000000 Number Took 419ms
------------------------------------------------------
Scanner With 7000000 Number Took 4895ms
UltraFastReader With 7000000 Number Took 275ms
InputReader With 7000000 Number Took 205ms
BufferReader With 7000000 Number Took 605ms
FastReader With 7000000 Number Took 495ms
------------------------------------------------------
Scanner With 8000000 Number Took 5592ms
UltraFastReader With 8000000 Number Took 316ms
InputReader With 8000000 Number Took 239ms
BufferReader With 8000000 Number Took 704ms
FastReader With 8000000 Number Took 565ms
------------------------------------------------------
Scanner With 9000000 Number Took 6303ms
UltraFastReader With 9000000 Number Took 342ms
InputReader With 9000000 Number Took 281ms
BufferReader With 9000000 Number Took 801ms
FastReader With 9000000 Number Took 630ms
------------------------------------------------------
Scanner With 10000000 Number Took 7076ms
UltraFastReader With 10000000 Number Took 396ms
InputReader With 10000000 Number Took 291ms
BufferReader With 10000000 Number Took 854ms
FastReader With 10000000 Number Took 683ms
------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment