2

I have a file with 100000 line when am using System.in it takes more than 1 minute to get the input but when I use a file to read the input it doesn't take time. what is the solution to keep using System.in but with more speed?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44

4 Answers4

2

System.in reads line by line not the file contents at a time.So it is slow.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
1

System.in is inherently slow because it's taking the data in line by line (checking for newlines), rather than doing a large block copy from a file mapped in virtual memory.

There's no real way to speed up System.in, and this sounds like a situation where reading a file would be much more ideal.

Update: You may want to look at this question: What's the fastest way to read from System.in in Java?

Community
  • 1
  • 1
Polynomial
  • 27,674
  • 12
  • 80
  • 107
0

You need to use StringTokenizer if you want to read file very fast. https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

0

Try to change the stream of system.in
System.setIn(new FileInputStream(fileName));

shift66
  • 11,760
  • 13
  • 50
  • 83