31st August 2017, 2 min read Parsing comma-separated integers in Java 2 thoughts on “Parsing comma-separated integers in Java” Jay says: November 13, 2017 at 8:26 pm Your manual implementation doesn’t even work. The results don’t correlate with the actual values; partially because of some false assumptions. I could go on but it was actually easier to write a working implementation with ~4x the throughput. m.l.m.p.ParseInt.manualSplit thrpt 5 12624.529 � 445.839 ops/s m.l.m.p.ParseInt.monolithicSplit thrpt 5 41031.703 � 5054.782 ops/s @Benchmark public int[] monolithicSplit(BenchmarkState s) { final String text = s.myarray; final int length = text.length(); final int limit = (length / 2) + 1; int[] array = new int[limit]; int pos = 0, tmp = 0; boolean neg = false; for (int i = 0; i < length; i++) { char c = text.charAt(i); if (c == ',') { array[pos++] = neg ? 0 – tmp : tmp; tmp = 0; neg = false; } else if (c == '-') { neg = true; } else { tmp = (tmp << 3) + (tmp << 1) + (c & 0xF); } } array[pos++] = neg ? 0 – tmp : tmp; int[] result = new int[pos]; System.arraycopy(array, 0, result, 0, pos); return result; } Daniel Lemire says: November 13, 2017 at 10:03 pm Your approach is indeed better than my manual hack and I have updated the code accordingly. Note that this was indicated in my post.
Your manual implementation doesn’t even work. The results don’t correlate with the actual values; partially because of some false assumptions. I could go on but it was actually easier to write a working implementation with ~4x the throughput.
m.l.m.p.ParseInt.manualSplit thrpt 5 12624.529 � 445.839 ops/s
m.l.m.p.ParseInt.monolithicSplit thrpt 5 41031.703 � 5054.782 ops/s
@Benchmark
public int[] monolithicSplit(BenchmarkState s) {
final String text = s.myarray;
final int length = text.length();
final int limit = (length / 2) + 1;
int[] array = new int[limit];
int pos = 0, tmp = 0;
boolean neg = false;
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
if (c == ',') {
array[pos++] = neg ? 0 – tmp : tmp;
tmp = 0;
neg = false;
} else if (c == '-') {
neg = true;
} else {
tmp = (tmp << 3) + (tmp << 1) + (c & 0xF);
}
}
array[pos++] = neg ? 0 – tmp : tmp;
int[] result = new int[pos];
System.arraycopy(array, 0, result, 0, pos);
return result;
}
Your approach is indeed better than my manual hack and I have updated the code accordingly. Note that this was indicated in my post.