java - Faster way than Scanner or BufferedReader reading multiline data from STDIN? -
note: coding in java. looking read input data string, 1 line @ time (or more), , expect lot of total lines.
right have implemented
scanner in = new scanner(system.in) while (in.hasnextline()) { separated = in.nextline().split(" "); ... }
because within line inputs space delimited.
unfortunately, millions of lines process slow , scanner taking more time data processing, looked java.io libraries , found bunch of possibilities , i'm not sure 1 use (bytearrayinputstream
, fileinputstream
, bufferedinputstream
, pipedinputstream
). 1 should use?
to specify, data being piped in text file, every line has either 4 or 6 words ended newline character, , need analyze 1 line @ time, setting (4 or 6) words array can temporarily manage. data format:
392903840 c b 293 32.90 382049804 c 390 329084203 d e r 489 384.90 ...
is there way scanner can read 1000 or lines @ time , become efficient or of these datatypes should use(to minimize speed)?
sidenote: while experimenting have tried:
java.io.bufferedreader stdin = new java.io.bufferedreader(new java.io.inputstreamreader(system.in)); while(in.ready()){ separated = in.readline().split(" "); ... }
which worked well, wondering 1 works best, , if there's way to, say, read 100 lines data @ once process everything. many options looking optimal solution.
you should wrap system.in
bufferinputstream
like:
bufferedinputstream bis = new bufferedinputstream(system.in); scanner in = new scanner(bis);
because minimises amount of reads system.in raises efficiency (the bufferedinputstream).
also, if you're reading lines, don't need scanner, reader (which has readline()
, ready()
methods new line , see if there's more data read).
you use such (see example @ java6 : inputstreamreader):
(i added cache size argument of 32mb bufferedreader
)
bufferedreader br = new bufferedreader(new inputstreamreader(system.in), 32*1024*1024); while (br.ready()) { string line = br.readline(); // process line }
from inputstreamreader doc page:
without buffering, each invocation of read() or readline() cause bytes read file, converted characters, , returned, can inefficient.
Comments
Post a Comment