Skip to content

Instantly share code, notes, and snippets.

@jhorstmann
Created July 28, 2019 19:38
Show Gist options
  • Save jhorstmann/9dcdc3c26a26e4ad6f513128942a47d9 to your computer and use it in GitHub Desktop.
Save jhorstmann/9dcdc3c26a26e4ad6f513128942a47d9 to your computer and use it in GitHub Desktop.
import java.io.*;
public class ReadLineBenchmark {
long volume = 0;
static class StringSlice implements CharSequence {
private final String str;
private final int idx;
private final int end;
StringSlice(String str, int idx, int end) {
this.str = str;
this.idx = idx;
this.end = end;
}
@Override
public int length() {
return end - idx;
}
@Override
public char charAt(int index) {
return str.charAt(idx + index);
}
@Override
public CharSequence subSequence(int start, int end) {
return new StringSlice(str, idx + start, idx + end);
}
@Override
public String toString() {
return str.substring(idx, end);
}
}
public void parseLine(String s) {
volume += s.length();
}
public void parseLine(CharSequence s) {
volume += s.length();
}
public static StringBuilder scanFile(String location) throws IOException {
FileReader fr = new FileReader(location);
BufferedReader bf = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
bf.lines().forEach(s -> sb.append(s).append('\n'));
bf.close();
return sb;
}
public void readString(String data) throws IOException {
StringReader fr = new StringReader(data);
BufferedReader bf = new BufferedReader(fr);
bf.lines().forEach(s -> parseLine(s));
bf.close();
}
public void readStringSubstring(String data) throws IOException {
int lastIdx = 0;
for (int idx = data.indexOf('\n'); idx > -1; idx = data.indexOf('\n', lastIdx)) {
parseLine(data.substring(lastIdx, idx));
lastIdx = idx + 1;
}
parseLine(data.substring(lastIdx));
}
public void readStringCustomSlice(String data) throws IOException {
int lastIdx = 0;
for (int idx = data.indexOf('\n'); idx > -1; idx = data.indexOf('\n', lastIdx)) {
parseLine(new StringSlice(data, lastIdx, idx));
lastIdx = idx + 1;
}
parseLine(new StringSlice(data, lastIdx, data.length()));
}
public void readStringSubsequence(String data) throws IOException {
int lastIdx = 0;
for (int idx = data.indexOf('\n'); idx > -1; idx = data.indexOf('\n', lastIdx)) {
parseLine(data.subSequence(lastIdx, idx));
lastIdx = idx + 1;
}
parseLine(data.subSequence(lastIdx, data.length()));
}
public void readStringLines(String data) {
data.lines().forEach(this::parseLine);
}
public static void main(String[] args) throws IOException {
StringBuilder stringBuffer = scanFile("test.txt");
String data = stringBuffer.toString();
for (int k = 0; k < 50; k++) {
ReadLineBenchmark d = new ReadLineBenchmark();
long bef = System.currentTimeMillis();
d.readStringCustomSlice(data);
long aft = System.currentTimeMillis();
long fz = new File("test.txt").length();
// we need the leave time for the JIT
if (k >= 40)
System.out.format("speed: %.3f GB/s\n", fz / (1024.0 * 1024.0 * 1024.0) / ((aft - bef) / 1000.0));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment