public
Created

  • Download Gist
ScannerTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import java.io.IOException;
import java.io.StringReader;
import java.util.Random;
import java.util.Scanner;
 
public class ScannerTest {
public static void main(String[] args) throws IOException {
int chars = (37 * 1000 * 1000);
 
StringBuilder buf = new StringBuilder(chars);
Random rand = new Random();
 
System.out.println("start appending " + chars + " chars");
while (buf.length() < chars) {
buf.append(rand.nextInt(9999)).append(" ");
}
 
System.out.println("buf to string");
String str = buf.toString();
 
for (int round = 0; round < 5; round++) {
 
Scanner scan = new Scanner(str);
 
long sum = 0;
 
System.out.println("scanning");
long start = System.currentTimeMillis();
while (scan.hasNext()) {
sum += scan.nextInt();
}
long time = System.currentTimeMillis() - start;
System.out.println("finished " + time + "ms " + sum);
 
System.out.println("reading");
StringReader r = new StringReader(str);
sum = 0;
 
int next;
int current = 0;
while ((next = r.read()) > 0) {
if (next == ' ') {
sum += current;
current = 0;
} else {
current = current * 10 + (next - '0');
}
}
time = System.currentTimeMillis() - start;
System.out.println("finished " + time + "ms " + sum);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.