Skip to content

Instantly share code, notes, and snippets.

@banthar
Created June 19, 2012 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banthar/2954231 to your computer and use it in GitHub Desktop.
Save banthar/2954231 to your computer and use it in GitHub Desktop.
java parser benchmark
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import java.util.Scanner;
public class Scan {
public static void main(String[] args) throws Exception {
PrintStream textOut = new PrintStream("data.txt");
DataOutputStream binOut = new DataOutputStream(new FileOutputStream("data.bin"));
Random r = new Random(0l);
for (int i = 0; i < 1000000; i++) {
int n = r.nextInt();
textOut.print(n);
textOut.print('\n');
binOut.writeInt(n);
}
textOut.close();
binOut.close();
{
long start = System.currentTimeMillis();
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
int sum = 0;
while (true) {
String line = reader.readLine();
if (line == null)
break;
sum += Integer.parseInt(line);
}
reader.close();
System.out.println("BufferedReader: " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
{
long start = System.currentTimeMillis();
Scanner s = new Scanner(new FileReader("data.txt"));
int sum = 0;
while (s.hasNext()) {
sum += s.nextInt();
}
s.close();
System.out.println("Scanner: " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
{
long start = System.currentTimeMillis();
FileChannel channel = FileChannel.open(Paths.get("data.txt"), StandardOpenOption.READ);
Scanner s = new Scanner(channel);
int sum = 0;
while (s.hasNext()) {
sum += s.nextInt();
}
channel.close();
System.out.println("Scanner(FileChannel): " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
{
long start = System.currentTimeMillis();
FileChannel channel = FileChannel.open(Paths.get("data.txt"), StandardOpenOption.READ);
MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
int sum = 0;
int n = 0;
int sign = 1;
for (int i = 0; i < buffer.limit(); i++) {
switch (buffer.get(i)) {
case '-':
sign = -1;
break;
case '\n':
sum += sign * n;
n = 0;
sign = 1;
break;
default:
n = n * 10 + buffer.get(i) - '0';
break;
}
}
channel.close();
System.out.println("MappedByteBuffer: " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
{
long start = System.currentTimeMillis();
FileChannel channel = FileChannel.open(Paths.get("data.bin"), StandardOpenOption.READ);
MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
IntBuffer intBuffer = buffer.asIntBuffer();
int sum = 0;
for (int i = 0; i < intBuffer.limit(); i++) {
sum += intBuffer.get(i);
}
channel.close();
System.out.println("IntBuffer: " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
{
long start = System.currentTimeMillis();
RandomAccessFile input = new RandomAccessFile("data.bin", "r");
int sum = 0;
int length = (int) (input.length() / 4);
for (int i = 0; i < length; i++) {
sum += input.readInt();
}
input.close();
System.out.println("RandomAccessFile: " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
{
long start = System.currentTimeMillis();
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream("data.bin")));
int sum = 0;
try {
while (true) {
sum += input.readInt();
}
} catch (EOFException e) {
}
input.close();
System.out.println("DataInputStream(BufferedInputStream): " + sum + " " + (System.currentTimeMillis() - start) + "ms");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment